Advertisement
Guest User

Untitled

a guest
Jan 13th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <iostream>
  2. // Function prototypes
  3. void removeSpecial(char[]);
  4. void ignoreCase(char[], int);
  5. bool isPalindrome(char[],int);
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     int   const size = 80;
  12.     char ch[size];
  13.     bool flag;
  14.    //user output
  15.     cout << "Enter a line that might be a palindrome:" << endl;
  16.     //user input character by character
  17.     cin.getline(ch,size);
  18.     cout << size << "since size shows up as 80, my isPalindrome will not work" << endl;
  19.  
  20.     removeSpecial(ch); // remove numbers 2154 and special characters @#$#$
  21.     ignoreCase(ch, size); //ignores capital or non capital for comparison
  22.  
  23.     flag = isPalindrome(ch, size);
  24.     /*
  25.     if (isPalindrome(ch, size) == true)
  26.  
  27.         cout << "The string "<<ch<<" is a palindrome" << endl;
  28.     else
  29.         cout << "The string " << ch << " is NOT a palindrome"<<endl;
  30.  
  31.     */
  32.     if (flag == true)
  33.  
  34.         cout << "The string " << ch << " is a palindrome" << endl;
  35.     else
  36.         cout << "The string " << ch << " is NOT a palindrome" << endl;
  37.  
  38.     return 0;
  39. }
  40.  
  41. //perfectly working is efficient?
  42. void removeSpecial(char ch[])
  43. {
  44.     int i, j;
  45.     for (i = 0; ch[i] != '\0'; ++i)
  46.     {
  47.         while (!   ( (ch[i] >= 'a' && ch[i] <= 'z') || (ch[i] >= 'A'   &&   ch[i] <= 'Z' || ch[i] == '\0') ) )
  48.         {
  49.             for (j = i; ch[j] != '\0'; ++j)
  50.             {
  51.                 ch[j] = ch[j + 1];
  52.             }
  53.             ch[j] = '\0';
  54.         }
  55.     }
  56.    
  57. }
  58.  
  59.  
  60. bool isPalindrome(char ch[], int size)
  61. {
  62.     bool flag = true;
  63.     for (int i = 0; i < size; i++)
  64.     {
  65.         if (ch[i] != ch[size - 1])
  66.             flag = false;
  67.         else
  68.             flag = true;
  69.  
  70.     }
  71.     return flag;
  72. }
  73.  
  74. //perfectly working
  75. void ignoreCase(char ch[],int size)
  76. {
  77.     for (int n = 0; n<size && ch[n] != '\0'; n++)
  78.     {
  79.         if (ch[n] >= 'a'&&ch[n] <= 'z')
  80.             ch[n] = ch[n] - 32;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement