Hello ruu,
"i" should be an "int" because it is used in a subscript. When you have "sent[i]" what is between the []s is called a subscript and is used to access a given element of an array. Even using a "std::string" you can use this to access each individual character of the string. Although a "char" is a type of "int" the number used in a "char" would most likely be larger than the array is.
An "int" can handle both positive (unsigned) and negative (signed) numbers where as an "unsigned int" can only handle the (unsigned) positive numbers. Since a subscript can only use positive numbers, negative are not allowed, making the variable an "unsigned int" to use only positive numbers makes sense.
While on the subject when I started working on the program I found that the for loop in the above code needed changed. It should read this way:
for (int i = static_cast<int>(sent.length()) - 1; i >= 0; i--) |
I do not use the "string.length()" in the first part of a for loop very often. so I did not think "- 1" part at first. "string.length()" returns the number of characters in a string, but the array starts at zero not 1, so the last character is at 3 not 4 that is why the "- 1". The ".length" function returns a "size_t" type variable. This is just another name for "unsigned int". The "static_cast<int>" is the newer C++ way to type cast one variable type into another variable type. I did this because of going backwards with the for loop I found that "i" would eventyally become "-1" before the loop ended and this was a problem when "i" was an "unsigned int".
That said the "reverse" function works.
I was thinking the program would benefit by changing any upper case letters to lower case letters before you reverse the string and most definitely before you check for palindrome. Include the header file <cctype> and you will have access to the functions "tolower(ch)" and "toupper(ch)" where "ch" refers to a single character. There are also other functions that are available. See:
http://www.cplusplus.com/reference/cctype/
And for the "std::string" see:
http://www.cplusplus.com/reference/string/string/
Two good links to bookmark for the future.
You may also consider a function that will remove any spaces and punctuation in the string.
You wrote:
to check whether a sentence ending with ‘.’ is a palindrome or not |
Unless you remove the '.' nothing you enter will be a palindrome because there will never be a match for the '.'. I suggest you leave the '.' out when prompting the user for input. Using a "std""string" to store the input in there is no need for a '.' to mark the end. But if you use the '.' be sure to remove it before any other processing takes place.
I did change some things around like making "char sent[100]; to a "std::string sent;" "sent" is a poor choice of the variable name. It gives the idea that it is being used for output not the input that you intend. For a variable like this I most often use "line" or "input" for the name. Using "line" with the function "std::getline(...)" makes sense because you are reading an entire up to a delimiter or "\n" newline. It may be my personal preference, but it is better to use a variable name describes what it does and it does make the code easier to read.
In main I removed the while loop because it is working against you also making the program flow more difficult. The while loop is trying to check individual letters in the "char array", but you need to check the whole string at one time.
Even with the "reversed" function I showed you the "isPalindrome" function will not work the way it is. "i" needs to be initialized before it is used. Even then the function will be checking the same character every time. In the if statement you are comparing a "char" to the returned value of "reversed" which is an "int". This is not likely to ever match because the value of the "char" is different than the value of the "int" that is returned.
I know that is a lot to think about and understand for now, so I will let you work on that. When you have a question let me know.
Hope that helps,
Andy