字符串处理2

  • 将包含字符数字的字符串分开,使得分开后的字符串前一部分是数字后一部分是字母
  • 将 字 符 串 中 的 空 格 替 换 成 “%020”
  • 删除字符串中指定的字符
  • 删除一个数组中重复的元素
  • 将 字 符 串 中 的 相 邻 的 多 余 空 格 去 掉
  • 求一个字符串数组的最大值和次大值
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//将包含字符数字的字符串分开,使得分开后的字符串前一部分是数字后一部分是字母
//例如“h1ell2o3” ->”123hello”
void disWord(){
    int i, bi, cnt;
    char a[50], b[50], tmp[50];
    while(gets(a) != NULL){
        i = 0; bi = 0; cnt = 0;

        while(a[i] != '\0'){
            if(a[i]>='0' && a[i]<='9'){
                b[bi] = a[i];
                bi++;
            }
            else{
                tmp[cnt] = a[i];
                cnt++;
            }
            i++;
        }
        b[bi] = '\0';
        tmp[cnt] = '\0';
        strcat(b, tmp);
        printf("%s\n", b);
    }
}

//将字符串中的空格替换成“%020”
void strRe(){
    char a[50], b[50];
    int i, j, tmp;
    while(gets(a) != NULL){
        i = j = 0;
        while(a[i] != '\0'){
            if(a[i] == ' '){
                b[j++] = '%';
                b[j++] = '0';
                b[j++] = '2';
                b[j++] = '0';
            }
            else{
                b[j] = a[i];
                j++;
            }
            i++;
        }
        b[j] = '\0';
        printf("%s\n", b);
    }
}

//删除字符串中指定的字符。 例如 “abcdaefaghiagkl“ 删除‘a’,以后:
//“bcdefghigkl”
void wordDel(){
    int i, j;
    char sig, ch;
    char *str = (char *)malloc(100*sizeof(char));
    char *after = (char *)malloc(100*sizeof(char));
    while(gets(str) != NULL){
        printf("%s,please input a alpha to del:", str);
        scanf("%c", &sig);
        i = j = 0;

        while(str[i] != '\0'){
            if(str[i] != sig){
                after[j] = str[i];
                j++;
            }
            i++;
        }
        after[j] = '\0';
        printf("The result is :%s\n", after);
        //printf("%d", getchar()); //缓冲区测试
        rewind(stdin); //清空缓存
    }
}

//删除一个数组中重复的元素。例如 1,2,2,2,3,3,3,4,4,5,5,5,6,6,6 ->
//1,2,3,4,5,6
void delRe(){
    int arr[50], after[50], flag[50]={0}, i, n, len=0;
    printf("please input n:");
    scanf("%d", &n);
    printf("input arr:\n");
    for(i=0; i<n; i++){
        scanf("%d", &arr[i]);
    }
    for(i=0; i<n; i++){
        if(flag[arr[i]] == 0){
            after[len] = arr[i];
            len++;
            flag[arr[i]] = 1;
        }
    }
    printf("The result is:");
    for(i=0; i<len; i++){
        printf("%d ", after[i]);
    }
    printf("\n");
}

//将字符串中的相邻的多余空格去掉,例如(空格用下划线表示):
//”___hello____world___how_are_you__”
//->”hello_world_how_are_you”
void strApt(){
    char a[50], b[50];
    int i, cnt, flag;
    while(gets(a) != NULL){
        i = cnt = flag = 0;
        while(a[i] != '\0'){
            if(a[i] != ' '){
                b[cnt] = a[i];
                cnt++;
                if(flag==1)
                    flag = 0;
            }
            else{
                if(flag==0){
                    if(i!=0){
                        b[cnt] = ' ';
                        cnt++;
                    }
                    flag = 1;
                }
            }
            i++;
        }
        if(b[cnt-1]==' ')
            cnt--;
        b[cnt] = '\0';
        printf("%s\n", b);
    }
}

//求一个字符串数组的最大值和次大值
void arrStr(){
    char *max, *max2, *arr[10];
    int i, num;
    max = (char*)malloc(50);
    max = (char*)malloc(50);
    printf("Please input str count:");
    scanf("%d", &num);
    fflush(stdin);
    arr[0] = (char*)malloc(50);
    gets(arr[0]);
    arr[1] = (char*)malloc(50);
    gets(arr[1]);
    if(strcmp(arr[0], arr[1]) > 0){
        max = arr[0];
        max2 = arr[1];
    }
    else{
        max2 = arr[0];
        max = arr[1];
    }
    for(i=2; i<num; i++){
        arr[i] = (char*)malloc(50);
        gets(arr[i]);
        if(strcmp(arr[i], max) > 0){
            max2 = max;
            max = arr[i];
        }
        else if(strcmp(arr[i], max2) > 0){
            max2 = arr[i];
        }
    }
    printf("The result is:\nmax:%s\nmax2:%s\n", max, max2);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值