| |||
曰。。#include<stdio.h> void main() { char m[1000]; int i,num=0,alpha=0,other=0,space=0; printf("请输入:"); scanf("%s",&m); for(i=0;m[i];i++) { if ('A'<=m[i] && m[i]<='Z'||'a'<=m[i] && m[i]<='z')alpha++; else if(m[i]==' ')space++; else if ('0'<=m[i] && m[i]<='9')num++; else other++; } printf("数字=%d,字母=%d,空格=%d,其他=%d",num,alpha,space,other); } | |||
大神日。。 #include<stdio.h> |
c++大神答。。。
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string str;
int iword=0,ispace=0,idigit=0;
cout<<"请输入一行字符"<<endl;
getline(cin,str);
for(string::size_type ix=0;ix!=str.size();++ix)
{
if(isalpha(str[ix])) ++iword;
else if(isspace(str[ix])) ++ispace;
else if(isdigit(str[ix])) ++idigit;
}
cout<<"字母有"<<iword<<"个"<<endl;
cout<<"空格有"<<ispace<<"个"<<endl;
cout<<"数字有"<<idigit<<"个"<<endl;
return 0;
}
#include<string.h>
#define SIZE 24
int main(void)
{
char str[SIZE];
char *i=str;
int ch=0,space=0,num=0,other=0;
gets(str);
for(;*i!='\0';i++)
{
if(*i>='a'&&*i<='z'||*i>='A'&&*i<='Z')
ch++;
else if (*i==' ')
space++;
else if(*i>='0'&&*i<='9')
num++;
else
other++;
}
printf("chars number is %d\n",ch);
printf("space number is %d\n",space);
printf("digit number is %d\n",num);
printf("other number is %d\n",other);
return 0;
}