统计用户输入 C语言

从键盘读取用户输入直到遇到#字符,编写程序统计读取的空格数目、读取的换行符数目以及读取的所有其他字符数目。(要求用getchar()输入字符)

#include

int main()

{

 printf("Please input a string end by #:\n");

 int c;

 int spaces = 0;

 int newlines = 0;

 int other = 0;

 while ((c = getchar()) != '#')

 {

  if (c == ' ')

  {

   spaces++;

  }

  else if (c == '\n')

  {

   newlines++;

 

  }

  else

  {

   other++;

 

  }

 }

 printf("spaces:%d\n", spaces);

 printf("nelines:%d\n", newlines);

 printf("other characters:%d\n", other);

 return 0;

 

}

你可能感兴趣的:(c语言)