【问题描述】
输入一个字符串,含有数字和非数字字符,如“sum=abc+234;while(abc==700)tab{ass=346++;bss=abc+267;}”,将其中连续的数字作为一个整数,依次存放到一个数组nums中。例如,234放在nums[0],700放在nums[1]……,统计共有多少个整数,并输出这些整数。结合指针和自定义函数完成该题。
【输入形式】sum=abc+234;while(abc==700)tab{ass=346++;bss=abc+267;
【输出形式】There are 4 integers:234 700 346 267
【样例输入】sum=abc+234;while(abc==700)tab{ass=346++;bss=abc+267;
【样例输出】There are 4 integers:234 700 346 267
按照题目给定的输入输出完成。
#include<bits/stdc++.h>
using namespace std;
void check (char*a)
{
char *end=NULL;
char temp[1000];
string number[1000];
int n=0;
for(int i;;i++)
{
while(*a!='\0')
{
if((*a>='0'&&*a<='9'))
{
end=a+1;
while((*end>='0'&&*end<='9'))
{
end++;
}
int t=0;
while(a<end)
{
temp[t]=*a;
t++;
a++;
}
temp[t]='\0';
number[n++]=temp;
break;
}
a++;
}
if(*a=='\0')
{
break;
}
}
cout<<"There are "<<n<<" integers:";
for(int i=0;i<n;i++)
{
cout<<number[i];
if(i<n-1)
{
cout<<" ";
}
}
}
int main()
{
char a[1000];
cin.getline(a,100);
check(a);
}
【问题描述】
输入一个字符串,含有数字和非数字字符,如“sum=abc+234;while(abc==700)tab{ass=346++;bss=abc+267;}”,将其中连续的数字作为一个整数,依次存放到一个数组nums中。例如,234放在nums[0],700放在nums[1]……,统计共有多少个整数,并输出这些整数;将输入字符串中的连续的字母作为一个单词存放到一个数组words中;如上面的字符串中,sum放在words[0],abc放在words[1]……,统计共有多少个单词,并输出这些单词。其它所有字符存放到一个数组others中,如上面的字符串中,=放在others[0],+放在others[1]……,统计共有多少个字符,并输出这些字符。结合指针和自定义函数完成该题。
【输入形式】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;}
【样例输出】
There are 4 integers:234 700 346 267
There are 8 words:sum abc while abc tab ass bss abc
There are 16 symbles:= + ; ( = = ) { = + + ; = + ; }
按照题目给定的输入输出完成。
#include<bits/stdc++.h>
using namespace std;
void check (char*a)
{
char *end=NULL;
char temp_1[1000];
char temp_2[1000];
string word[1000];
string number[1000];
char other[1000];
int w=0,n=0,o=0;
for(int i;;i++)
{
while(*a!='\0')
{
if((*a>='a'&&*a<='z')||(*a>='A'&&*a<='Z'))
{
end=a+1;
while((*end>='a'&&*end<='z')||(*end>='A'&&*end<='Z'))
{
end++;
}
int t=0;
while(a<end)
{
temp_1[t]=*a;
t++;
a++;
}
temp_1[t]='\0';
word[w++]=temp_1;
break;
}
else if((*a>='0'&&*a<='9'))
{
end=a+1;
while((*end>='0'&&*end<='9'))
{
end++;
}
int t=0;
while(a<end)
{
temp_2[t]=*a;
t++;
a++;
}
temp_2[t]='\0';
number[n++]=temp_2;
break;
}
else
{
if(*a!=' ')
{
other[o++]=*a;
}
}
a++;
}
if(*a=='\0')
{
break;
}
}
cout<<"There are "<<n<<" integers:";
for(int i=0;i<n;i++)
{
cout<<number[i];
if(i<n-1)
{
cout<<" ";
}
}
cout<<endl;
cout<<"There are "<<w<<" words:";
for(int i=0;i<w;i++)
{
cout<<word[i];
if(i<w-1)
{
cout<<" ";
}
}
cout<<endl;
cout<<"There are "<<o<<" symbles:";
for(int i=0;i<o;i++)
{
cout<<other[i];
if(i<o-1)
{
cout<<" ";
}
}
}
int main()
{
char a[1000];
cin.getline(a,100);
check(a);
}