Help with palindrome

A palindrome is a word, phrase, verse, or sentence that reads the same backward or forward. For example: A man, a plan, a canal, Panama! Ask the user for a string and check to see if it is a palindrome. (Remove all the spaces and punctuation before you check. Also make sure that you convert everything into the same case, upper or lower case, before you check. After the palindrome check, if there are any digits in the string, extract them one by one to create a number. (Example: The string “12Heh21” is a palindrome and the number is 1221.) Take the number and multiply it by your age and display the results.

My question is what function would i use to check if its a palindrome
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//somthing to start with.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
    cout << "Enter a word or a sentence: ";
    string word;
    bool pal = true;
    int j = 0;

    while(word.length()==NULL)
	{
           cout<<">>";
            getline(cin, word);
			if(word.length()==NULL)
			{
			cout<<"You entered nothing try again."<<endl;
			}
	}
     for (int i = word.size()-1; i >= 0; i--)
         {
           
               
               
               if(toupper(word[j]) != toupper(word[i]))
               {
                pal = false;  
                break;
               }
			   
	        j++;
		   
		   
          }

  if(pal==false)
	  cout<<word<<" not a palindrome."<<endl;
  else
	  cout<<word<<" is a palindrome."<<endl;
   
	main();
		
 return 0;
}
Last edited on
Give this a try.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void runner(void);
int main()
{
	string choice;
	do
	{
	runner();
	cout<<"Want to run this again ? \ny for yes n for no : ";
	getline(cin,choice);
	}
	while(choice =="y" || choice =="Y");
		
 return 0;
}
void runner (void)
{


	    cout << "Enter a word or a sentence: ";
    string word;
	string newword= "";
	string digits = "";
    bool pal = true;
    int j = 0;
	
    while(word.length()==NULL)
	{
           cout<<">>";
            getline(cin, word);
			if(word.length()==NULL)
			{
			cout<<"You entered nothing try again."<<endl;
			}
	}
	
	for(int i = 0 ; i < (word.size());i++)
	{
		if(word[i] == ' ' || word[i] == '.'||word[i] == ','||word[i] == '!'||word[i] == '?')
		{
			

			
		}else
		{

			newword+=tolower(word[i]);
		}
		if(isdigit(word[i]))
		{

			digits+= word[i];

		}
		

	}
	word = newword;
     for (int i = word.size()-1; i >= 0; i--)
         {
           
               
               
               if(word[j] != word[i])
               {
                pal = false;  
                break;
               }
			   
	        j++;
		   
		   
          }

  if(pal==false)
	  cout<<word<<" not a palindrome."<<endl;
  else
	  cout<<word<<" is a palindrome."<<endl;
   char *pend;
   int age =2;
  cout<< endl <<"Value = "<<digits<<endl; 
  cout<<"Value times age = "<<strtod(digits.c_str(),&pend) * age<<endl<<endl ;

		



}
Last edited on
Topic archived. No new replies allowed.