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

    //题目57:编写一个业务函数,实现字符串(前后各有三个空格,单词前后也均有空格)
    //"   i am student, you are teacher   " ,
    //各个单词首字符大写,结果如下"   i am student,  you are teacher   " ,
    //要求1:实现所有接口  70
    //要求2:写出测试程序  30
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    //需求解析:实现英语句子中各个单词首字符大写(字符串不确定)
    //思路:①遍历句子中所有的字符,找出所有的空格
    //②只要发现非空格,就将字符一个个存入数组中,直到遇到空格位置,生成一个字符串数组,数组中存储所有的单词
    //③ 重新拼接英文句子
    
    //处理英语句子
    int ProtectEnglish(const char * pinstr/*in*/, char *pout/*in*/){
        int ERRO_MSG = 0;
        if (pinstr == NULL || pout == NULL)
        {
            ERRO_MSG = 1;
            printf("pinstr == NULL || pout==NULL erro msg:%d
    ", ERRO_MSG);
            return ERRO_MSG;
        }
        int i = 0, j = 0,k=3;
        //获取字符串的长度
        int index = strlen(pinstr);
        int index2 = index;
        //定义返回字符串
        char *resstr = pout;
        memset(resstr, 0, sizeof(char)*(index + 1));
        //定义单词个数
        int numx = 0;
        //定义存储字符串的二维数组
        char **ptemp1 = (char **)malloc(sizeof(char *));
        //定义单词个数
        int num2 = 0;
        //定义单词中字符个数
        int num = 1;
        //分配单个单词存数数组
        char *ptemp2 = NULL;
        while (index--){
            if (*pinstr!=' ')
            {
                char tempc = 0;
                if (num==1)
                {
                    ptemp2 = (char *)malloc(sizeof(char));
                    //处理非字母字符
                    if ((int)*pinstr>96 && (int)*pinstr<123)
                    {
                        //首字母大写
                        tempc = (int)*pinstr - 32;
                    }
                    else{
                        tempc = *pinstr;
                    }
                    
                }
                else{
                    //重新分配内存空间
                    ptemp2 = (char *)realloc(ptemp2, sizeof(char)*(num));
                    tempc = *pinstr;
                }
                //存储单个字符
                ptemp2[num - 1] = tempc;
                num++;
            }
            else{
                //当遇到空格时开始计数下一个单词
                if (num>1)
                {
                    //确保每个单词都是字符串
                    ptemp2 = (char *)realloc(ptemp2, sizeof(char)*(num));
                    ptemp2[num - 1] = '';
                    if (num2>0)
                    {
                        ptemp1 = (char **)realloc(ptemp1, sizeof(char *)*(num2+1));
                    }
                    //将上一个单词加入数组
                    ptemp1[num2] = ptemp2;
                    num2++;
                    //开始下一个单词
                    num = 1;
                }
            }
            
            pinstr++;
        }
        //重新拼接单词
        if (num2>0)
        {
            //前后各有三个空格,单词前后也均有空格
            resstr[0] = ' ';
            resstr[1] = ' ';
            resstr[2] = ' ';
            resstr[index2 - 1] = ' ';
            resstr[index2 - 2] = ' ';
            for (i = 0; i < num2; i++)
            {
                //判断每个单词的长度
                //暂时没有想到其他设计可以把每个单词填充到一个字符数组里
                int num3 = strlen(ptemp1[i]);
                for (j = 0; j < num3; j++)
                {
                    resstr[k] = ptemp1[i][j];
                    k++;//数组的自增设计--适用与根据内部条件自增
                }
                resstr[k] = ' ';
                k++;
                //释放内存
                free(ptemp1[i]);
                ptemp1[i] = NULL;
            }
            //释放内存
            free(ptemp1);
            ptemp1 = NULL;
        }
        return ERRO_MSG;
    }
    
    void main(){
        char *english = "   i am student , you are teacher   ";
        char *pstr = (char *)malloc(sizeof(char)*(strlen(english) + 1));
        ProtectEnglish(english, pstr);
        printf("执行之后的字符串是%s
    ", pstr);
        free(pstr);
        pstr = NULL;
        system("pause");
    }

  • 相关阅读:
    Linux下安装Flume
    [译]MIT6.824_1.1分布式系统介绍——驱动力与挑战
    MySQL-Canal-Kafka数据复制详解
    Linux下搭建Kafka集群
    我在创业公司的云原生之旅
    使用go向es进行数据操作脚本
    kubectl exec 向pod的文件中增加内容
    kubernetes资源导出小脚本
    面试题(四) -- 第一次当面试官
    gitlab备份检查小脚本
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5597146.html
Copyright © 2011-2022 走看看