这个程序的主要问题是它不会计算字符串中空格的数量,即使它应该在遇到空格时减少计数(它开始时将 count 设置为字符串的长度)。我没有正确检查空格(通过检查''),还是我的递归案例有问题?
# include <stdio.h>
# include <string.h>
// function to reverse string and count its length
int rPrint(char *str, int count)
{
if(*str)
{
if(*str != ' ')
rPrint(str+1, count);
else
rPrint(str+1, count - 1);
printf("%c", *str);
}
return count;
}
int main()
{
char string[28] = "";
int count = 0;
printf("Please enter a string: ");
gets(string);
count = rPrint(string, strlen(string));
printf("\nThe number of non-blank characters in the string is %d.", count);
}