编写一个函数,把字符串中的数字字符、英文字母字符和其它字符分开。

编写一个函数,把字符串中的数字字符、英文字母字符和其它字符分开。如对于字符串“12a:?3byu98!”,将其分解为三个独立的字符串“12398”、“abyu”和“:?!”。要求主函数输入原字符串,分解后的三个字符串在主函数打印输出。

#include <iostream>
#include <map>
#include <string>
using namespace std;

map<string, string> fun1(string* p_str);

int main() {

    string str;
    cout << "输入字符串:" << endl;
    getline(cin,str);

    map<string, string> maps;
    maps = fun1(&str);

    map<string, string>::iterator iter = maps.begin();
    while (iter != maps.end()) {
        cout << iter->first << "  " << iter->second << endl;
        iter++;
    }


    return 0;
}



map<string,string> fun1(string* p_str) {
    string temp = *p_str;
    int sum = 0;
    for (int i = 0; i < temp.length(); i++) {
        if ( (temp[i] >= 'A' && temp[i] <= 'Z') || ( temp[i] >='a' && temp[i] <='z' ) ) {
            sum++; continue;
        }
        if (temp[i] >='0' && temp[i]<='9') {
            sum++; continue;
        }
        sum++;
    }
    
    map<string,string> maps;
    maps.insert(pair<string,string> ("英文字符",""));
    maps.insert(pair<string, string>("数字字符", ""));
    maps.insert(pair<string, string>("其它字符", ""));

    for (int i = 0; i < temp.length(); i++) {
        if ((temp[i] >= 'A' && temp[i] <= 'Z') || (temp[i] >= 'a' && temp[i] <= 'z')) {
            string value = maps["英文字符"];
            maps["英文字符"] = value + temp[i];
            continue;
        }
        if (temp[i] >= '0' && temp[i] <= '9') {
            string value = maps["数字字符"];
            maps["数字字符"] = value + temp[i];
            continue;
        }
        string value = maps["其它字符"];
        maps["其它字符"] = value + temp[i];
        continue;;
    }

    return maps;

}

遍历字符串,根据字符值范围,分别存到3个字符数组即可

#include <stdio.h>
void fun(char *s,char *y,char *z,char *k)
{
    int i=0,j=0,m=0,n=0;
    while(s[i] != 0)
    {
        if(s[i] >='0' && s[i] <='9')
            z[m++] = s[i];
        else if((s[i] >='a' && s[i] <='z') || (s[i]>='A' && s[i]<='Z'))
            y[j++] = s[i];
        else
            k[n++] = s[i];
        i++;
    }
    y[j] = z[m] = k[n] = 0;
}
int main()
{
    char s[1000],y[1000],z[1000],k[1000];
    gets(s);
    fun(s,y,z,k);
    puts(y);
    puts(z);
    puts(k);
    return 0;
}

建三个字符数组

img

#include<stdio.h>
int c1=0,c2=0,c3=0;
void fun(char d[],char a[],char b[],char c[]) 
{
    int j=0;
    while(d[j]!='\0')
    {
        if ( (d[j] >= 'A' && d[j] <= 'Z') || ( d[j] >='a' && d[j] <='z' ) ) 
        {
            a[c1]=d[j];
            c1++;
        }
        else if(d[j]<='9'&&d[j]>='0')
        {
            b[c2]=d[j];
            c2++;
        }
        else
        {
            c[c3]=d[j];
            c3++;
        }
        j++;
    }
}
int main()
{
    char a[250],b[250],c[250],d[750];

    gets(d);
    int j=0;
    fun(d,a,b,c);
    printf("\n数字字符:");
    for(j=0;j<c2;j++)
    {
        printf("%c",b[j]);
    }

    printf("\n英文字母:");
    for(j=0;j<c1;j++)
    {
        printf("%c",a[j]);
    }

    printf("\n其它字符:");
    for(j=0;j<c3;j++)
    {
        printf("%c",c[j]);
    }
    printf("\n");

    return 0;
}


//input 输入字符串;output1 输出数字字符串;output2 输出字母字符串; output3 输出其他字符串
bool split(char *input, char *output1, char *output2, char *output3)
{
    char *p,*p1,*p2,*p3;
    if(input || output1 || output2 || output3)  return false;
    p=input;
    p1=output1;
    p2=output2;
    p3=output3;
    while(*p!=0)
    {
        if(*p>='0' && *p<='9') *(p1++)=*p;
        else if((*p>='A' && *p<='Z') || (*p>='a' && *p<='z')) *(p2++)=*p;
        else *(p3++)=*p;
        p++;
    }
    return true;
}

供参考:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
void  Disassemble_string(char* s, char* digit, char* letter, char* other)
{
    char* p = s;
    while (*p) {
        if (isdigit(*p))
            *digit++ = *p++;
        else if (isalpha(*p))
            *letter++ = *p++;
        else
            *other++ = *p++;
    }
    *digit = '\0'; *letter = '\0'; *other = '\0';
}
int main()
{
    char str[128], digit[128], letter[128], other[128];
    gets(str);
    Disassemble_string(str, digit, letter, other);
    puts(digit);
    puts(letter);
    puts(other);
    return 0;
}

定义三个数组存数字,字符,字母。
通过assic码判断是否数字,字母,其他就是字符。
然后存到对应数组,打印输出