C语言怎么处理scanf();后有换行符的问题?
入门题目:字符串分类统计题目描述:输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。
每次一回车,比预想的多一字符!
2022-11-10 06:21
程序代码:
/**********************************
换行符、制表符。。。归为‘其它字符’
**********************************/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int lettes = 0;
int numbers = 0;
int spaces = 0;
int symbols = 0;
char str = '\0';
do{
scanf("%c", &str);
if( str >= 'a' && str <= 'z' )
{ lettes++; }
else if( str >= 'A' && str <= 'Z')
{ lettes++; }
else if( str >= '0' && str <= '9' )
{ numbers++; }
else if( str == ' ')
{ spaces++; }
else
{ symbols++; }
}while( str != '\n' );
printf("字母%d个,数字%d个,空格%d个,其它字符%d个\n", lettes, numbers, spaces, symbols);
system("pause");
return 0;
}
[此贴子已经被作者于2022-11-10 08:43编辑过]

2022-11-10 08:41

2022-11-10 08:48
[此贴子已经被作者于2022-11-10 09:06编辑过]

2022-11-10 09:04
2022-11-10 09:08



[此贴子已经被作者于2022-11-10 09:40编辑过]

2022-11-10 09:22