华为历年机试题型总结系列(二)

本文介绍了三种实用的字符串处理方法:字符压缩算法,将连续重复字符压缩并以数字表示;字串分离技术,用特定符号替换空格;首字母大写转换,使每个单词首字母变为大写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3. 字符串压缩——连续出现次数大于等于2的字符,压缩后出现数字信息

    输入: aabbbcc  输出:2a3b2c          输入:abcdef        输出:abcdef

    PS:数字2转换成对应的字符2为: (char)(2 + 48); 

<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>

void StrZip(char *pInputStr, int StrLength, char *pOutputStr)
{
      int i,j=0,words_count=1;
      for(i=0;i<StrLength;)
      {
           if(pInputStr!='\0' && pInputStr[i]==pInputStr[i+1])     //如果连续出现字符
            {
                  ++words_count;
                  ++i;
             }
            
             if(words_count==1)    //如果只出现一次
             {
                   pOutputStr[j++]=pInputStr[i++];
              }else             //如果连续出现
              {
                      pOutputStr[j++]=(char)(words_count+48);    //将数字转化成对应的字符
                      pOutputStr[j++]=pInputStr[i++];
                      words_count=1;   //计数归1,便于后继判断
              }
      }

      pOutputStr[j]='\0';
}

int main(void)
{
     char pInputStr[20],pOutputStr[20];
     int StrLength;

     while(scanf("%s",pInputStr) != EOF)
     {
             StrLength=strlen(pInputStr);
             StrZip(pInputStr,StrLength,pOutputStr);
             puts(pOutputStr);
      }

            return 0;
}




4. 字串分离

    输入:a b c d   输出:a,b,c,d, 将空格用‘,’代替,并且最后一个字符后要有‘,’

#include<stdio.h>
#include<string.h>

void StrDivide(char *pInputStr, int StrLength, char *pOutputStr)
{
     int i,j=0;
     for(i=0;i<StrLength;)
     {
           if(pInputStr[i]==' ')   //如果遇到空格
           {
                pOutputStr[j++]=',';
                ++i;
           }else                   //非空格字符直接赋值输出
           {
                pOutputStr[j++]=pInputStr[i++];
           }
     }

           pOutputStr[j]=',';  //最后一个字符后面是,
           pOutputStr[j+1]='\0';
}

int main(void)
{
     char pInputStr[20],pOutputStr[20];
     int StrLength;

     printf("Input the string:\n");
     gets(pInputStr);
     StrLength=strlen(pInputStr);

     StrDivide(pInputStr,StrLength,pOutputStr);
     puts(pOutputStr);

     return 0;
}

5. 字符串首字母转换成大写

    输入:this is a book 输出:This Is A Book

    PS:小写转换成大写 CH=ch-'a'+'A';

#include<stdio.h>
#include<string.h>

void FirstLetter(char *pInputStr, int StrLength, char *pOutputStr)
{
       int i,j=1;
       pOutputStr[0]=pInputStr[0]-'a'+'A';    //首字母大写
        for(i=1;i<StrLength;)
        {
              if(pInputStr[i]==' ')   //如果遇到空格
              {
                    pOutputStr[j++]=pInputStr[i];   //输出空格
                    pOutputStr[j++]=pInputStr[i+1]-'a'+'A';         //空格后的字母大写
                    i+=2;      //原字符串指针跳过空格和大写字母
               }else
               {
                    pOutputStr[j++]=pInputStr[i++];        //其余字符直接输出
               }
        }
             pOutputStr[j]='\0';
}

int main(void)
{
     char pInputStr[20],pOutputStr[20];
     int StrLength;

     printf("Input the string:\n");
     gets(pInputStr);

     FirstLetter(pInputStr,StrLength,pOutputStr);
     puts(pOutputStr);

     return 0;
}

       下面接 华为历年机试题型总结系列(三)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值