Palindromes

I'm writing a simple program that can check for words or phrases and determines if it is a Palindrome or not. A palindrome is a word or phrase that reads the same backward as forward. Here are some examples of palindromes:

Radar (can read it backward or forward)
Madam!I'm adam

Unfortunately, everything I input came out as "It is not a palindrome", I'm not sure what I did wrong as I followed step by step very closely as in the guild line:

1. Create a string variable
2. Convert the string to uppercase characters
3. Keep only the alphabetic characters.
4. Compare the first character with the last character. If they're are the same, compare the next character.

Can someone please check my codes and tell me what wrong with it, thank you.
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
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

int main()
{
    char str[80], again;
    int strlength, i,j=0;
    int front=0,back; int flag=1;
	 gets(str);

do {
	cout << "Input a string: ";
	cin >> str;
    
//Get the length of string str
	strlength = strlen(str);

//Convert all the characters in the string str to uppercase
 	for(int i = 0; i < strlength; i++)
       {
		  str[i] = toupper(str[i]);
		  }
		 
//Remove all the special characters except letters a - z
    for(int j = 0; j < strlength; j++)
     {
        if (isalpha(str[i]))
       {str[j] = str[i];
       	j++;
         }
    }    
str[j]= '\0';

//front index is pointed to the first character in the string str
    front = 0;

//back index points to the last character in the string str
    back = strlength - 1;

//Compare the first character with the last character. If they're are the same,
//compare the next character.
    for(int i=0; i< strlength/2;i++)
    {
        if(str[front] != str[back])
        {
            flag = 0;
            break;
        }
        front++;
        back=back -1;
    }

    	if(flag == 0)
		cout<<"It is not a palindrome"<<endl;
		
		else
		cout<<"It's a palindrome"<<endl;
		
//Do-While loop until user exit				
		cout <<"Input another string (y/n)?";  
  		cin >> again; 
 } while (again == 'Y' || again == 'y');
 return 0;
}

A few points:
1. line 12 is not needed since you do this when you enter the loop. Though this will not make you get the wrong result.

2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Convert all the characters in the string str to uppercase
 	for(int i = 0; i < strlength; i++)
       {
		  str[i] = toupper(str[i]);
		  }
		 
//Remove all the special characters except letters a - z
    for(int j = 0; j < strlength; j++)
     {
        if (isalpha(str[i]))
       {str[j] = str[i];
       	j++;
         }
    }    
str[j]= '\0';

Something is very wrong here. You've defined i and j at the top of your code. So you can use them all over your program and you've redefined them in your loops. This might be a reason why your program doesn't give you the desired result.

2.2 in the second loop: j++ should be i++ since j is incremented in your loop header.
Though when you correct this code, it would not do what you want, a-z characters are written back to the string, special characters are not, but you don't delete them. so this is what happens in memory if you have a string M@TLAB:
M -> executed if code
@ -> if is not executed
T -> executed if code
L -> executed if code

From your code str[1] will still be @. Perhaps use a second variable to copy the value to without the special characters.
Thank you joeriMJ, I modified the code again and look like this
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
int main()
{
char str[80], again;
int strlength, i,j=0;
int front=0,back; int flag=1;


do {
cout << "Input a string: ";
cin.getline(str,80); //use cin.getline to read a string with spaces.
cout<<str<<endl; //Test for original string


//Get the length of string str
strlength = strlen(str);

//Convert all the characters in the string str to uppercase
	for(i = 0; i < strlength; i++)
		{if (islower(str[i])) //test if it's a lower case
		str[i] = toupper(str[i]);
		}
cout<<str<<endl; //CHECK IF LETTERS ARE IN THE UPPER CASE

//Remove all the special characters except letters a - z
	for(i = 0; i < strlength;i++)
		{
		if (isalpha(str[i])) // check for alphabetical in string i
			{
			str[j] = str[i]; // copy it to string j
			j++;
			}
		}
str[j]= '\0';
cout<<str<<endl; //TEST to make sure there isn't any special characters or space

//front index is pointed to the first character in the string str
front = 0;

//back index points to the last character in the string str
back = strlength - 1;

//Compare the first character with the last character. If they're all the same,
//compare the next character.
for(i=0; i< j/2 ;i++)
	{
		if(str[front] != str[back])
		{
		flag = 0;
		break;
		}
		front++;
		back=back - 1;
	}

	if(flag == 0)
	cout<<"It is not a palindrome"<<endl;

	else
	cout<<"It's a palindrome"<<endl;

//Do-While loop until user exit
	cout <<endl;
	cout <<"Input another string (y/n)?";
	cin >> again;
	cin.ignore(); //Skip the line
 } while (again == 'Y' || again == 'y');
return 0;
}


However, when I ran it. Here is what I have
1
2
3
4
5
6
7
8
9
10
11
12
Input a string: abc123@#
abc123@#
ABC123@#
ABC
It is not a palindrome

Input another string (y/n)? y
Input a string: abc123$%
abc123$%
ABC123$%
ABCABCABCABC
It is not a Palindrome


It's obviously something wrong with my Do-While loop....anyone have any ideal on how to fix this?
Topic archived. No new replies allowed.