Practice Program
Solution:
void main()
{
int result ,i;
char s1[20],s2[20];
printf("Enter the string: ");
gets(s1);
strcpy(s2,s1);
int l = strlen(s1);//storing length of string in variable l.
char ch;
for(i=0; i<(l/2); i++)
{
ch = s1[i]; //at every iteration we swapping character at i index
s1[i] = s1[(l-1)-i]; //with character at index ((l-1)-i)
s1[(l-1)-i] = ch;
}
if(strcmp(s1,s2) == 0)
{
printf("String is palindrome");
}
else
printf("String is not palindrome.");
}
void main() { int result ,i; char s1[20],s2[20]; printf("Enter the string: "); gets(s1); strcpy(s2,s1); int l = strlen(s1);//storing length of string in variable l. char ch; for(i=0; i<(l/2); i++) { ch = s1[i]; //at every iteration we swapping character at i index s1[i] = s1[(l-1)-i]; //with character at index ((l-1)-i) s1[(l-1)-i] = ch; } if(strcmp(s1,s2) == 0) { printf("String is palindrome"); } else printf("String is not palindrome."); }
Output1
Enter the string: programmerdouts
String is not palindrome
String is not palindrome
Output2
Enter the string: LeveL
String is palindrome
String is palindrome
Output3
Enter the string: racecar
String is palindrome
String is palindrome
defining a function 'is_palindrome' which will check string is palindrome or not
int is_palindrome(char s1[])
{
int l = strlen(s1);//storing length of string in variable l.
int i;
char s2[20];
strcpy(s2,s1);
char ch;
for(i=0; i<(l/2); i++)
{
ch = s1[i]; //at every iteration we swapping character at i index
s1[i] = s1[(l-1)-i]; //with character at index ((l-1)-i)
s1[(l-1)-i] = ch;
}
if(strcmp(s1,s2) == 0)
{
return 1;
}
else
return 0;
}
int is_palindrome(char s1[]) { int l = strlen(s1);//storing length of string in variable l. int i; char s2[20]; strcpy(s2,s1); char ch; for(i=0; i<(l/2); i++) { ch = s1[i]; //at every iteration we swapping character at i index s1[i] = s1[(l-1)-i]; //with character at index ((l-1)-i) s1[(l-1)-i] = ch; } if(strcmp(s1,s2) == 0) { return 1; } else return 0; }
Using above define function 'is_palindrome()':
void main()
{ int result;
char s[20] = "level";
result = is_palindrome(s);
if(result == 1)
{
printf("String is palindrome");
}
else
printf("String is not palindrome.");
}
void main() { int result; char s[20] = "level"; result = is_palindrome(s); if(result == 1) { printf("String is palindrome"); } else printf("String is not palindrome."); }
Output1
String is palindrome
Using above define function 'is_palindrome()':
void main()
{ int result;
char s[20] = "doubts";
result = is_palindrome(s);
if(result == 1)
{
printf("String is palindrome");
}
else
printf("String is not palindrome.");
}
void main() { int result; char s[20] = "doubts"; result = is_palindrome(s); if(result == 1) { printf("String is palindrome"); } else printf("String is not palindrome."); }
Output2
String is not palindrome
Using above define function 'is_palindrome()':
void main()
{ int result;
char s[20] = "programmer";
result = is_palindrome(s);
if(result == 1)
{
printf("String is palindrome");
}
else
printf("String is not palindrome.");
}
void main() { int result; char s[20] = "programmer"; result = is_palindrome(s); if(result == 1) { printf("String is palindrome"); } else printf("String is not palindrome."); }
Output3
String is not palindrome
0 Comments
Post a Comment