Strings

This is a discussion on Strings within the C++ Programming forums, part of the General Programming Boards category; Hello, What I am trying to do is create the classic palindrome detection program. I am having quite some trouble ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Strings

    Hello,
    What I am trying to do is create the classic palindrome detection program. I am having quite some trouble to get it working properly. The problems I am having are:

    1) Whenever a string evaluates as a palindrome, every string after that is never a palindrome.
    2) In my posted code, I am using cin >> input instead of getline(cin, input) because using the getline gives me weird results for my program. If anyone has any ideas why, that'd be great.

    Here is the brains of the operation:

    Code:
        
    cin >> input;
    	while (input[0] != '0') {
    
    		if (input[20]) {
    			cout << "Your sentence/phrase can only be 20 characters long." << endl;
    			cout << "Try again..." << endl;
    		}
    
    		back = input.length() - 1;
    
    		while (done == false) {
                if (input[front] == input[back]) {
                    front++;
    				back--;
    				cout << "Testing 1" << endl;
    			} else {
    				palindrome = false;
                    done = true;
    				cout << "Testing 2" << endl;
    			}
    			if (front >= back) {
    				done = true;
    				cout << "Testing 3" << endl;
    			}
    
    		}
    
        if (palindrome == true) {
    	   cout << input << " is a palindrome."  << endl;
    	} else {
    	   cout << input << " is not a palindrome." << endl;
    	}
    
    	palindrome = true;
    	done = false;
    	cin >> input;
    	}
    
        cout << "Ending program..." << endl;
    
    
    	return 0;
    }
    As you can tell, I have inserted some cout testing statements as I was trying to pinpoint my problem.

    As I progess, I need to discard whitespaces and accept capital letters, but as of right now, I am just trying to get the darn thing to work. I appreciate any ideas. Thank you.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Pretty much got it working. The problem was the front not being initalized to zero when a new word would be entered.

    For the discarding whitespaces, I'm sure there is an isspace command.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21