このプログラムの主な問題は、空白に遭遇した場合にカウントをデクリメントすることになっているにもかかわらず、文字列内の空白の数をカウントしないことです(カウントは文字列の長さに設定されています)。空白を正しくチェックしていませんか(''をチェックして)、または再帰の場合に何か問題がありますか?
# 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);
}