Posted By

kemikazii on 11/27/15


Tagged


Versions (?)

Palindrome


 / Published in: C++
 

A program that takes a string and tests it to see if it is a palindrome or not

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<string>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9.  
  10. string pal;
  11. cout<<"Please enter a word to test: \n\n"<<endl;
  12.  
  13. getline(cin,pal);//inputs the word to be tested
  14.  
  15. size_t position = pal.find(' ',0);
  16.  
  17. while(position!=string::npos){//uses a loop to remove any spaces from the string that has been inputted
  18. pal.erase(position,1);
  19. size_t newPos = position+1;
  20. position = pal.find(' ',newPos);
  21. }
  22.  
  23.  
  24. string newstring = pal;
  25. reverse(pal.begin(),pal.end());//creates a second string that is the intial in reverse
  26.  
  27. if(newstring.compare(pal)==0) cout<<"\nThis word is a Palindrome\n"<<endl;//compares both strings to see if the intial string is a palindrome
  28. else cout<<"This word is not a palindrome"<<endl;
  29.  
  30.  
  31. system("pause");
  32. return 0;
  33.  
  34. }

Report this snippet  

You need to login to post a comment.