Advertisement
Shailrshah

Palindrome Test and frequency

Apr 19th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int is_palindrome(int start, int end, char *str)
  5. {
  6.     if (start>=end)
  7.         return 1;
  8.     if (str[start]!= str[end])
  9.         return 0;
  10.     return is_palindrome(++start,--end,str);
  11.  }
  12.  int frequency(int end, char *str, char c)
  13.  {
  14.     int fr=0;
  15.     int i;
  16.     for(i=0;i<end;i++)
  17.         if(str[i]==c)
  18.             fr+=1;
  19.     return fr;
  20.  }
  21. int main()
  22. {
  23.     char str[20],c;
  24.     printf("Enter the String.\n");
  25.     fgets( str, sizeof( str ), stdin );
  26.     if(is_palindrome(0, strlen(str)-2, str))
  27.         printf("It's a palindrome!\n");
  28.     else
  29.         printf("It's not a palindrome! \n");
  30.     printf("Enter the character you want to search for: ");
  31.     scanf("%c",&c);
  32.     printf("The frequency is %d",frequency((strlen(str)-1),str,c));
  33.     return 0;
  34. }
  35. //Output
  36. //Enter the String.
  37. //ABBA
  38. //It's a palindrome!
  39. //Enter the character you want to search for: A
  40. //The frequency is 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement