Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.

Similar presentations


Presentation on theme: "Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length."— Presentation transcript:

1 Strings program

2 C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length = 0, flag = 0; fflush(stdin); printf("Enter a string: \n"); gets(string); /* keep going through each character of the string till its end*/ for (i = 0; string[i] != '\0'; i++) { length++; } for (i = length - 1; i >= 0; i--) {

3 reverse_string[length - i - 1] = string[i]; } /* * Compare the input string and its reverse. If both are equal * then the input string is palindrome. */ for (i = 0; i < length; i++) { if (reverse_string[i] == string[i]) flag = 1; else flag = 0; } if (flag == 1) printf("%s is a palindrome \n", string); else printf("%s is not a palindrome \n", string); } OUTPUT Enter a string : malayalam malayalam is a palindrome

4 Length of the string #include int main() { char s[1000],i; printf("Enter a string: "); scanf("%s",s); for(i=0; s[i]!='\0'; ++i); printf("Length of string: %d",i); return 0; } OUTPUT: Enter a string: Programiz Length of string: 9

5 SORTING OF ARRAY ELEMENTS #include main() { int a[25], i, n, j,t; clrscr(); printf(“Enter the number of elements not exceeding 25”); scanf(“%d”, &n); for (i=0;i<n;i++) { printf(“\n Enter the element”); scanf(“%d\n”, &a[i]); }

6 for(i=0;i<n;i++) { for (j=i+1;j<n;j++) { if(a[i]>a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } getch(); }


Download ppt "Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length."

Similar presentations


Ads by Google