| whitesaya (7) | |||
|
I have an assignment where i have to make a palindrome program. As you can see i have already done the basic palindrome prog, however i am now stuck since the program goes into a infinite loop whenever a word with whitespaces is added. I have also tried searching for methods online however they do not seem to work for me...
| |||
|
Last edited on
|
|||
| JLBorges (1756) | |||
| |||
|
|
|||
| whitesaya (7) | |
| Hey really appreciate the help! Would you be so kind as to explain what does your for loop do, cause i don't really understand it. | |
|
|
|
| JLBorges (1756) | |
|
> explain what does your for loop do, cause i don't really understand it. The for loop for( int i = 0 ; p > i ; ++i, --p ) is identical to the one that you wrote; the variable names i and p in your original were deliberately retained, and have the same meaning. The difference is in the two while loops. while( p>i && ( isspace(cstr[i]) || ispunct(cstr[i]) ) ) ++i ;If the next character (the one at position i) is one to be skipped , increment i till we hit a character that is not a white space or punctuation. while( p>i && ( isspace(cstr[p]) || ispunct(cstr[p]) ) ) --p ; is identical, except that we decrement p. The ++i, --p in the for loop get to the next pair of characters in the string; the while loops skip characters till a character to be considered for comparison is encountered.if( toupper( cstr[i] ) != toupper( cstr[p] ) ) return false ;If these two characters do not match, we have determined that the string is not a palindrome; there is nothing more to be done. | |
|
|
|