编写函数,统计字符串中字母,数字,空格和其他字符的个数,要求在主函数中输入字符串,并输入统计的结果
3个回答
展开全部
//////////////////////////////////////////////
/* 测试环境vc6.0 */
//////////////////////////////////////////////
#include<iostream>
#include<cstring>
using namespace std;
#define MAXLONG 100 //定义最大字符长度100
int* Find(char string[]) //计数函数,返回数组首地址
{
int symbol[5]={0};
for(int i =0; i < strlen(string);i++)
{
if (string[i] >= '0'&&string[i] <= '9')
{
symbol[0]++;
}
else if ((string[i] >= 'A'&&string[i] <= 'Z')||(string[i] >= 'a'&&string[i] <= 'z'))
{
symbol[1]++;
}
else if(string[i] ==' ')
{
symbol[2]++;
}
else symbol[3]++;
}
cout<<" "<<symbol[3];
return symbol;
}
int main (void)
{
char string[MAXLONG];
int *p;
int number; //数字个数
int letter; //字母个数
int blank; //空格个数
int others; //其他个数
cout<<"Please input the char string:";
cin.getline(string,MAXLONG,'\n');
p = Find(string);
number =*p;
letter =*(p+1);
blank = *(p+2);
others = *(p+3);
cout<<"number:"<<number<<endl;
cout<<"letter:"<<letter<<endl;
cout<<"blank:"<<blank<<endl;
cout<<"others:"<<others<<endl;
return 0;
/* 测试环境vc6.0 */
//////////////////////////////////////////////
#include<iostream>
#include<cstring>
using namespace std;
#define MAXLONG 100 //定义最大字符长度100
int* Find(char string[]) //计数函数,返回数组首地址
{
int symbol[5]={0};
for(int i =0; i < strlen(string);i++)
{
if (string[i] >= '0'&&string[i] <= '9')
{
symbol[0]++;
}
else if ((string[i] >= 'A'&&string[i] <= 'Z')||(string[i] >= 'a'&&string[i] <= 'z'))
{
symbol[1]++;
}
else if(string[i] ==' ')
{
symbol[2]++;
}
else symbol[3]++;
}
cout<<" "<<symbol[3];
return symbol;
}
int main (void)
{
char string[MAXLONG];
int *p;
int number; //数字个数
int letter; //字母个数
int blank; //空格个数
int others; //其他个数
cout<<"Please input the char string:";
cin.getline(string,MAXLONG,'\n');
p = Find(string);
number =*p;
letter =*(p+1);
blank = *(p+2);
others = *(p+3);
cout<<"number:"<<number<<endl;
cout<<"letter:"<<letter<<endl;
cout<<"blank:"<<blank<<endl;
cout<<"others:"<<others<<endl;
return 0;
展开全部
C++:
#i nclude<stdio.h>
int letter,digit,space,others;
void main()
{
void count(char[]);
char text[80];
printf("输入字符串:\n");
gets(text);
printf("字符串是:");
puts(text);
letter=0;
digit=0;
space=0;
others=0;
count(text);
printf("letter:%d,digit:%d,space:%d,others:%d\n",letter,digit,space,others);
}
void count(char str[])
{int i;
for(i=0;str[i]!='\0';i++)
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
letter++;
else if(str[i]>='0'&&str[i]<='9')
digit++;
else if(str[i]==32)
space++;
else
others++;
}
#i nclude<stdio.h>
int letter,digit,space,others;
void main()
{
void count(char[]);
char text[80];
printf("输入字符串:\n");
gets(text);
printf("字符串是:");
puts(text);
letter=0;
digit=0;
space=0;
others=0;
count(text);
printf("letter:%d,digit:%d,space:%d,others:%d\n",letter,digit,space,others);
}
void count(char str[])
{int i;
for(i=0;str[i]!='\0';i++)
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
letter++;
else if(str[i]>='0'&&str[i]<='9')
digit++;
else if(str[i]==32)
space++;
else
others++;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询