// 73.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "stdio.h" #include "string.h" int main(int argc, char* argv[]) { char s[100]; int alp, space, num, flag, i, word; flag = 1; alp = 0; space = 0; num = 0; word = 0; while( gets(s) != NULL) { for(i=0; i<(int)strlen(s); i++) { if( s[i]==32 ) //空格 { space++; if(flag == 1) word++; flag = 0; } else if( ( s[i]>=65 && s[i]<=90 ) || ( s[i]>=97 && s[i]<=122 )) //字母 { alp++; flag = 1; } else if( s[i]>=48 && s[i]<=57 ) //数字 { num++; flag = 1; } } if( s[0] == 32) word--; if (s[strlen(s)-1] == 32) word--; printf("%d %d %d %d\n",space,num,alp,++word); flag = 1; alp = 0; space = 0; num = 0; word = 0; } return 0; }
【4】功能:从stdio流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为null值,并由此来结束字符串。