NotAlpha102

Palindrome (Traversing with pointers)

Jul 10th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<string.h>
  4. #include<stdbool.h>
  5. void RemoveChars(char *s, char c);
  6. main()
  7. {
  8.     char str[50], *ptr, *p, *i, *j;
  9.     bool is_palindrome=true;
  10.     gets(str);
  11.  
  12.     RemoveChars(str, ' ');
  13.     RemoveChars(str, '.');
  14.     RemoveChars(str,'-');
  15.     RemoveChars(str, '!');
  16.     RemoveChars(str, '?');
  17.     RemoveChars(str, ',');
  18.     RemoveChars(str, ';');
  19.     RemoveChars(str, ':');
  20.     int n=strlen(str);
  21.  
  22.     for( p=str; p<str+n; p++)
  23.     {
  24.         *p=toupper(*p);
  25.  
  26.  
  27.     }
  28.     for( i=str, j=str+n-1; i<str+n, j>=str; i++, j--)
  29.     {
  30.         if(*i!=*j)
  31.         {
  32.             is_palindrome=false;
  33.         }
  34.     }
  35.     if(is_palindrome)
  36.     {
  37.         printf("It is a palindrome");
  38.     }
  39.     else
  40.         printf("It is not a palindrome");
  41.  
  42.  
  43.  
  44.  
  45. }
  46. void RemoveChars(char *s, char c)
  47. {
  48.     int writer = 0, reader = 0;
  49.  
  50.     while (s[reader])
  51.     {
  52.         if (s[reader]!=c)
  53.         {
  54.             s[writer] = s[reader];
  55.             writer++;
  56.         }
  57.  
  58.         reader++;
  59.     }
  60.  
  61.     s[writer]=0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment