String Palindrome With Loops - stuck!

I absolutely am stuck, I'm so close to finishing this, I've messed up somewhere along the way unfortunately. Any help would be appreciated if you'd be so kind. A second opinion never hurts!

I'm just a beginner so excuse any lack of knowledge. I may have made this ultimately harder than it needs to be, but it's how I was able to solve this puzzle.

I'm trying to get it to check an inputted string to see if when it's rearranged backwards, it matches, ignoring white space and non letter characters.

(assume includes and all are present)

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
  void stringtest()

{
	string input, output;

	cout<<"Enter a string:  ";
	cin.ignore(80, '\n');
	getline(cin, input);
	cout<<endl;

	int length, totalL, lengthnew, runs;
        lengthnew=0;
	totalL=lengthnew-1;

	runs=0;
	length = input.length(); //total length

	output=input; //used to make uppercase

	for(int h=0; h<=length-1; h++)
	{
		output[h] = toupper(input[h]); //new string with all uppercase
	}

    string nospaces;
    nospaces="";
    	for(int i=0; i<length; i++)
	{
		if(isalpha(output[i])) //testing to see if there is non letters
			nospaces+=output[i]; //adds the letters to a new string
	}

    lengthnew = nospaces.length(); //total string length without whitespace or non letters

	for(int i=0; i<=nospaces.length()-1; i++)
	{
		if(nospaces[i] == nospaces[totalL--])  //compares i (starts at beginning) to totalL-- (starts at end)
		{
			runs = runs++;	//checks total times it's ran through, if runs successful it adds, and should equal length
		}
	}

		if(runs == totalL)
			cout<<"Yes the string is a palindrome.\n";
		else
			cout<<"No the string is not a palindrome.\n";


    menu();

}

Last edited on
I think that totalL doesn't have any specific value, at the lne 12 totalL=lengthnew-1 at the time lenghnew doesn't have a value,
Line 39 should read runs++;

Line 13 means nothing. Remember, computers do things in order. You are asking the computer to do something with garbage data. (The computer won't "remember" this as an equation, because it is an assignment statement.)
Move the line down to line 34.

Line 43: Didn't you just modify the value of totalL? Why would it be equal to the length of nospaces? (Use nospaces.length().)

BTW, you have got way too many variables in there, which is contributing to your confusion. (Too many variables == trying to keep too many things in your mind at once == mistakes.)

Once you get the program working, try to reduce the number of variables you need. (Don't forget to backup first!)

Hope this helps.

[edit]BTW. Nice solution!
Last edited on
Topic archived. No new replies allowed.