char str[]
says that the parameter str is an array of characters, but the size is unspecified. The parameter could also be specified as
char *str
for the same effect, but using [] is a nice hint to the programmer to say "this points to an array of items, not just one.'
Function charCount() doesn't actually work because the loop at lines 33-37 doesn't change currentChar. You may be thinking "but wait! It updates count, and since currentChar is str[count], shouldn't that work?" The answer is no because line 31 assigns the
value of str[count] to currentChar, not the
formula. When count changes, currentChar stays the same. CharCount should be something like this:
1 2 3 4 5 6 7
|
int CharCount(const char*str) {
int count=0;
while (*str++) {
++count;
}
return count;
}
|
removeSpaces() and reverseChar() define an array whose length is determined at runtime. This is non-standard C++. You don't actually need these arrays at all since you're outputting the value. Just output as you go. This has the added advantage of meaning that you don't need to call countChars() at all.
Since the
else
clause from lines 54-57 is empty, you don't need it, although I know some people who put an
else
with every
if
, just to force them to think about the
else
case.
Taken together, removeSpaces would look like this:
1 2 3 4 5 6 7 8
|
void removeSpaces(const char str*)
{
for (; *str; ++str) {
if (*str != ' ') {
cout << *str;
}
}
}
|
reverseChar could also be done without copying the array. I'll leave that one to you.