for (int i = 0; i <= *s; i++)
*s will get the character at the start of the C-string s. For example, if s == "Hello", *s == 'H'. It will then be implicitly converted to an int, resulting in the ASCII value of 'H', 72. As you don't increment s, your for loop is essentially the same as doing
for( int i = 0; i <= 72; i++ )
On a similar note,
if(isupper(s + i) == 1)
s == 72, and as long as i <= 18 (ASCII character of 'Z' is 90), this expression will return true.
This expression will most likely return false, as you're passing a pointer (an address in memory), which when converted into an int, will be an extremely large value. It's outside the range of the ASCII values of uppercase characters(65 to 90, inclusive).
Also, you isupper() isn't guaranteed to return 1, if it's true.
A value different from zero (i.e., true) if indeed c is an uppercase alphabetic letter. Zero (i.e., false) otherwise. |
Coincidentally, that's how C++ evaluates expressions like this
if( 100 )
. This expression will always be true. However,
if( 0 )
will always be false.
Basically, you can just write
if( isupper( /* ... */ ) )
.
tolower(s + i);
tolower() returns its argument's lowercase variant if it has one.
Return Value
The lowercase equivalent to c, if such value exists, or c (unchanged) otherwise.
The value is returned as an int value that can be implicitly casted to char. |
http://www.cplusplus.com/reference/cctype/tolower/
Now onto how to solve your problem.
Notice that this problem can be split up into 2 sub-problems:
- Reversing the string
- Flipping the case of each character
If you know how to reverse a string, that's the hardest part of your problem solved.
The second sub-problem is really easy. Your code demonstrated that you understood what you needed to do.
1 2 3 4 5 6 7 8
|
if(isupper(s + i) == 1)
{
tolower(s + i);
}
else
{
toupper(s + i);
}
|