【输入形式】sum=abc+234;while(abc==700)tab{ass=346++;bss=abc+267;}
【输出形式】
There are 4 integers:234 700 346 267
There are 8 words:sum abc while abc tab ass bss abc
There are 16 symbles:= + ; ( = = ) { = + + ; = + ; }
输入样例:sum=abc+234;while(abc==700)tab{ass=346++;bss=abc+267;}
本题不要求识别,输出空格,所以输出时加个 ( !=‘ ’)就行
核心要点是用指针遍历数组,找到符合条件的字符再向后继续遍历直到不满足条件。
#include <iostream>
#include <string.h>
using namespace std;
void shabi(char*a);
int main()
{
char array[100];
cin.getline(array,100);
shabi(array);
}
void shabi(char*a)
{
char* jiesu=NULL;
int gg=0;
char str_1[100];
int numbs[100];
char others[100];
string words[100];
int numbs_sum=0;
int word_sum = 0;
int other_sum = 0;
for(int i=0;;i++)
{
while(*a!='\0')
{
if((*a>='a'&&*a<='z')||(*a>='A'&&*a<='Z'))
{
jiesu=a+1;
while((*jiesu>='a'&&*jiesu<='z')||(*jiesu>='A'&&*jiesu<='Z'))
{
jiesu++;
}
int len = 0;
// 记录一下 str 的 len
while(a<jiesu)
{
str_1[len++]=*a;
a++;
}
str_1[len] = '\0';
words[word_sum++] = str_1; //通过char型数组存入字符串数组实现存入词组
break;
}
else if(*a>='0'&&*a<='9')
{
jiesu=a+1;
while(*jiesu>='0'&&*jiesu<='9')
{
jiesu++;
}
while(a<jiesu)
{
gg=gg*10+(int)*a-'0'; //通过个十百位乘10相加实现存入数字
a++;
}
numbs[numbs_sum++]=gg;
gg=0;
break;
}
else
{
if(*a!=' ') others[other_sum++] = *a;
}
a++;
}
if(*a=='\0') break;
}
//输出数字
cout<<"There are "<<numbs_sum<<" integers:";
for(int j=0;j<numbs_sum;j++)
{
cout<<numbs[j];
if(j!=numbs_sum-1) cout<<" ";
}
cout<<endl;
//输出词组
cout<<"There are "<<word_sum<<" words:";
for(int j=0;j<word_sum;j++)
{
cout<<words[j];
if(j!=word_sum-1) cout<<" ";
}
cout<<endl;
//输出其余字符
cout<<"There are "<<other_sum<<" symbles:";
for(int j = 0; j < other_sum; j++)
{
cout << others[j];
if(j!=other_sum-1) cout<<" ";
}
}
本人才疏学浅,如有错误,烦请大佬们指正