zoukankan      html  css  js  c++  java
  • C语言 百炼成钢26

    /*
    题目62: 有一下特征字符串"eerrrrqqAB33333ABa333333ABjsfdsfdsa"
    编写一个业务函数,
    实现功能1:实现按照子串"AB"分割字符串,把“eerrrrqq”,"33333","a333333","jsfdsfdsa"
            把实现结果按照二维数组(第2种内存模型)打包传出。
    实现功能2:对二维数组(第二种内存模型),进行排序输出
    要求1:请自己编写业务一个接口(函数),并实现功能;70分
    要求2:编写测试用例。30分
    要求3:自己编写内存释放函数
    */
    
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    /*
    分析:
    使用strstr函数可以很快找出AB的地址,把AB替换成  这个前面一段就会成为一个新的字符串
    */
    
    //分割字符串
    int SpitAndSortStr(char *pin/*in*/, char(*pout)[100]/*out*/,int *rnum){
        int ERRO_MSG = 0;
        if (pin == NULL || pout == NULL || rnum==NULL)
        {
            ERRO_MSG = 1;
            printf("pin == NULL || pout==NULL || rnum==NULL 传入参数不可以为空 erro msg:%d
    ", ERRO_MSG);
            return ERRO_MSG;
        }
        char *pstart = pin;
        int index = 0;
        char *temp = strstr(pstart, "AB");
        while (temp != NULL){
            *temp = '';
            strcpy(pout[index], pstart);
            index++;
            //指针向后移动2个字节
            pstart = temp + 2;
            temp=strstr(pstart, "AB");
        }
        strcpy(pout[index], pstart);
        *rnum = index + 1;
        return ERRO_MSG;
    }
    
    //字符串排序
    int sortstr(char (*pin)[100],int num){
        int ERRO_MSG = 0;
        if (pin==NULL)
        {
            ERRO_MSG = 1;
            printf("传入参数不可以为空 erro msg:%d
    ", ERRO_MSG);
            return ERRO_MSG;
        }
        int i = 0,j=0;
        char temp[100] = {0};
        for (int i = 0; i < num; i++)
        {
            for (j = i + 1; j < num; j++)
            {
                if (strcmp(pin[i],pin[j])>0)
                {
                    strcpy(temp, pin[i]);
                    strcpy(pin[i], pin[j]);
                    strcpy(pin[j], temp);
                }
            }
        }
        return ERRO_MSG;
    }
    
    //打印二维数组
    void print(char(*pin)[100],int num){
        if (pin==NULL)
        {
            printf("传入的参数不可以为空!
    ");
            return;
        }
        int i = 0;
        for (i = 0; i < num; i++)
        {
            printf("%s
    ", pin[i]);
        }
    }
    
    void main(){
        char str[100] = "eerrrrqqAB33333ABa333333ABjsfdsfdsa";
        char arr[5][100] = { 0 };
        int ret = 0,num=0;
        ret = SpitAndSortStr(str, arr,&num);
        if (ret!=0)
        {
            printf("分割字符串程序执行出错!
    ");
        }
        //打印字符串
        print(arr, num);
        printf("
    ------------排序后---------------------
    ");
        ret = sortstr(arr, num);
        if (ret != 0)
        {
            printf("排序程序执行出错!
    ");
        }
        //打印字符串
        print(arr, num);
        printf("程序执行完毕!
    ");
        system("pause");
    }

  • 相关阅读:
    Linux makefile教程之概述一[转]
    Valid Parentheses
    Letter Combinations of a Phone Number
    机器学习经典分类算法 —— C4.5算法(附python实现代码)
    3Sum Closest
    3Sum
    Integer to Roman
    寒假文献阅读(四)
    Longest Common Prefix
    Roman to Integer
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5658176.html
Copyright © 2011-2022 走看看