Enter a string and fetch the integer of implementation code

  • 2020-04-02 01:25:30
  • OfStack

Enter a string containing all numeric and non-numeric characters. The consecutive Numbers are stored as an integer in an array to count the number of integers and output them.

#include<iostream>
using namespace std;
int main(){
    int a[30]={0};
    char str[200];
    cout<<" Please enter a string with a number n"<<endl;
    cin>>str;
    bool flag=0;
    int i=0,j=0;
    int s=0;
    int num=strlen(str);
    cout<<"n You've typed in all of them :"<<num<<" A character "<<endl;
    for(i=0;i<num;){
        while(str[i]>='0' && str[i]<='9' &&i<num ){
            s=s*10+int(str[i])-int('0');
            i++;
            flag=1;
        }
        if(flag==1){
            a[j++]=s;
            s=0;
            flag=0;
        }
        i++;
    }
    i=0;
    cout<<"n The total "<<j<<" A digital n"<<endl;
    while(i<j){
        cout<<a[i]<<endl;
        i++;
    }
    cout<<endl;
    return 0;
}

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090209251212.jpg ">


Related articles: