#include<stdio.h>
int main(void)
{
int y=0,k=0,s=0,q=0,i;
char ch;
printf("输入15个字符:");
for(i=1;i<=15;i++)
{
ch=getchar();
if('a'<=ch&&ch<='z'||'A'<=ch&&ch<='Z')
y++;
else
if(ch==''||ch=='\n')
k++;
else
if('0'<=ch&&ch<='9')
s++;
else
q++;
}
printf("英文字母的个数=%d;空格或回车的个数=%d;数字字符的个数=%d;其他字符的个数=%d\n",y,k,s,q);
return 0;
}
include用法:
#include命令预处理命令的一种,预处理命令可以将别的源代码内容插入到所指定的位置;可以标识出只有在特定条件下才会被编译的某一段程序代码;可以定义类似标识符功能的宏,在编译时,预处理器会用别的文本取代该宏。
插入头文件的内容
#include命令告诉预处理器将指定头文件的内容插入到预处理器命令的相应位置。有两种方式可以指定插入头文件:
1、#include<文件名>
2、#include"文件名"
#include
#include
#define MAXS 15
char* ReadString (char s[]) {
gets (s);
return s;
}
void StringCount (char s[]){
char *p = s;
int letter, blank, digit, other; /*分类计数*/
letter = blank = digit = other = 0;
while (*p != '\0') {
if ( (*p>='A'&&*p<='Z') || (*p>='a'&&*p<='z') )
letter++;
else if (*p == ' ')
blank++;
else if (*p>='0'&&*p<='9')
digit++;
else
other++;
p++;
}
printf ("letter = %d, blank = %d, digit = %d, other = %d\n",letter, blank, digit, other);
putchar ('\n');
}
int main(void) {
char s[MAXS];
ReadString(s);
StringCount(s);
return 0;
}
运行结果
#include
#define MAXS 15
void StringCount(char s[]);
int main()
{
char s[MAXS];
StringCount(s);
return 0;
}
void StringCount(char s[])
{
int i,n=0,m=0;
int letter=0,blank=0,digit=0,other=0;
for(i=0;i<15;i++)
{
scanf("%c",&s[i]);
if(s[i] == '\n') n++;
if(n==2) break;
m++;
}
for(i=0;i
if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z'))
letter++;
else if(s[i]>='0'&&s[i]<='9')
digit++;
else if(s[i]==' '|| s[i] == '\n')
blank++;
else
other++;
}
printf("letter = %d, blank = %d, digit = %d, other = %d",letter,blank,digit,other);
}
void StringCount (char s[]){
char *p = s;
int letter, blank, digit, other;
letter = blank = digit = other = 0;
while (*p != '\0') {
if ( (*p>='A'&&*p<='Z') || (*p>='a'&&*p<='z') )
letter++;
else if (*p == ' '||*p=='\n')
blank++;
else if (*p>='0'&&*p<='9')
digit++;
else
other++;
p++;
}
printf ("letter = %d, blank = %d, digit = %d, other = %d\n",letter, blank, digit, other);
}