#include <stdio.h>
#include <conio.h>
int main(void)
{
int ia, icount = 0;
ia = getchar();
while (ia != '\n')
{
if (ia == ' ')
{
icount++;
}
ia = getchar();
}
if (icount != 0)
{
icount++;
}
printf("icount = %d\n", icount);
getch();
return 0;
}
楼主要的应该是这个样子
#include <stdio.h>
#define N 80
int Count(char *str);
int main(void)
{
char str[N];
int n;
printf("Enter a string: ");
fgets(str, N, stdin);
n = Count(str);
printf("n = %d\n", n);
return 0;
}
int Count(char *str)
{
int n = 0, flag = 0;
while (*str != '\n')
{
if (*str == ' ' && flag == 0)
{
n++;
flag = 1;
}
if (*str != ' ')
{
flag = 0;
}
str++;
}
if (flag == 0)
{
n++;
}
return n;
}