printOriginal looks pretty good, but you could shorten it up:
1 2 3 4
|
void printOriginal(char /*const*/ s[])
{
while (*s) std::cout << *s++ << '\n';
}
|
To print a string backwards, you can use the following straightforward algorithm.
1. Let
len be the length of the string
s.
2. If
len is zero (the string is empty), terminate.
3. Otherwise, Let
i = len - 1 be the index of the last character in the string.
4. Print the
i'th character of the string
s[i]
5. Let
i = i - 1.
6. If
i is nonnegative (i is a valid index into the string s), go to step 4.
7. Otherwise, terminate.
Work on step 1.
Can you write the function
int myStrlen(char s[])
That counts the number of characters in a string?
The terminating NUL character only exists to signal the end of the string, so don't include it in the count.