【C++】输入一个字符串,含有数字和非数字字符,将连续的数字作为一个整数,依次存放到一个数组nums中;将连续的字母作为一个单词存放到一个数组words中;其它所有字符存放到一个数组others中。

【输入形式】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<<" ";
    }
}

本人才疏学浅,如有错误,烦请大佬们指正

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值