字符串处理

1.通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。

要求实现函数: 
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr:  输入字符串
         lInputLen:  输入字符串长度         
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例 
输入:“deefd”        输出:“def”
输入:“afafafaf”     输出:“af”
输入:“pppppppp”     输出:“p”

void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{
   int a[26]={0};
   int pos=0;
   long i,j;
   for(i=0,j=0;i<lInputLen;i++)
      {
        pos=pInputStr[i]-'a';
        if(a[pos]==0)
          {
            a[pos]++;
            pOutputStr[j++]=pInputStr[i];
           }
       }
}

2.通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
压缩规则:
1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"

要求实现函数: 
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr:  输入字符串
         lInputLen:  输入字符串长度         
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例 
输入:“cccddecc”   输出:“3c2de2c”
输入:“adef”     输出:“adef”
输入:“pppppppp” 输出:“8p”
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr)
{
   long i=0,j=0,k=0;
   int count;
   while(i<lInputLen)
     {
        count=0;
        while(pInputStr[i]==pInputStr[j])
         {
           count++;
           j++;
          }
        if(count!=1)
         {
            pOutputStr[k++]=count+'0';
            pOutputStr[k++]=pInputStr[i];
          }
        else
            pOutputStr[k++]=pInputStr[i];
        i=j;
     }
}

3.通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。

补充说明:
1. 操作数为正整数,不需要考虑计算结果溢出的情况。
2. 若输入算式格式错误,输出结果为“0”。

要求实现函数: 
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);

【输入】 pInputStr:  输入字符串
         lInputLen:  输入字符串长度         
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;

【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

示例 
输入:“4 + 7”  输出:“11”
输入:“4 - 7”  输出:“-3”
输入:“9 ++ 7”  输出:“0” 注:格式错误
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
   int a,b,count=0;
   long i=0,j=0,k=0;
   char tempa[MAXCHAR]={0};
   char tempb[MAXCHAR]={0};
   char ch;
   bool bl=true;
   while(i<lInputLen)
   {
     if(i!=0&&count<2&&pInputStr[i]==' '&&
       (pInputStr[i+1]=='+'||pInputStr[i+1]=='-')||isdigit(pInputStr[i+1]))
           count++;
     else if(count==0&&isdigit(pInputStr[i]))
           tempa[j++]=pInputStr[i];
     else if(count==1&&(pInputStr[i]=='+'||pInputStr[i]=='-')&&
            (pInputStr[i+1]!='+'&&pInputStr[i+1]!='-'))
           ch=pInputStr[i];
     else if(count==2&&isdigit(pInputStr[i]))
           tempb[k++]=pInputStr[i];
     else 
          bl=false;
      i++;
    }
    if(bl==true&&count==2)
     {
       a=atoi(tempa);
       b=atoi(tempb);
       switch(ch)
        {
           case '+':
               itoa(a+b,pOutputStr,10);
               break;
           case '-':
               itoa(a-b,pOutputStr,10);
               break;
           default:
               break;
         }
    }
   else
      pOutputStr[0]='0';
}

4.题目描述:通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空格分隔。请编写一个程序,自动分离出各个子串,并使用’,’将其分隔,并且在最后也补充一个’,’并将子串存储。  如果输入“abc def gh i    d”,结果将是abc,def,gh,i,d, 
  
要求实现函数:void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr); 

【输入】 pInputStr: 输入字符串    lInputLen:  输入字符串长度           
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长; 
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出    
 示例   输入:“abc def gh i    d” 输出:“abc,def,gh,i,d,”
void DivideString(const char *pInputStr, long lInputLen, char *pOutputStr)
{
    long i=0,j=0;
    while(i<lInputLen)
     {
      if(pInputStr[i]!=' ')
        {
          while(pInputStr[i]!=' ')
              pOutputStr[j++]=pInputStr[i++];
          pOutputStr[j++]=',';
         }
       else
         i++;
      }
}


5.题目描述:输入一个字符串,将其中大写字母转换为对应小写字母之后的第五个字母,若原始大写字母为V~Z, 则转换为对应小写字母的值减21。 其他字符不变,输出转换后的字符串。例如,对于字母A,则转换为小写字母f;若形参是字母W,则转换为小写字母b  
 
要求实现函数:void TransferString(const char * pInputStr, long lInputLen, char * pOutputStr);   

【输入】 pInputStr:  输入字符串            lInputLen:  输入字符串长度            
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;   
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出  
 
 示例  输入:“Axs3mWss”   输出:“fxs3mbss”

void TransferString(const char * pInputStr, long lInputLen, char * pOutputStr)

{
     long i,j=0;
     for(i=0;i<lInputLen;i++)
       {
         if(pInputStr[i]>='A'&&pInputStr[i]<='U')
            pOutputStr[j++]=pInputStr[i]+37;
         else if(pInputStr[i]>='V'&&pInputStr[i]<='Z')
            pOutputStr[j++]=pInputStr[i]+11;
         else
            pOutputStr[j++]=pInputStr[i];
        }
}

6.问题描述:将一个字符串中的大写字母顺序颠倒过来,并输出。字符串的长度不超过100个字符。

要求实现函数:void StringReverse(char* pInputStr, char* pOutputStr)

【输入】pInputStr:输入的字符串
【输出】pOutputStr:输出转换好的逆序字符串
【返回】无
 
 举例: 输入input=“I am A stUdent”,输出output=“UAI”

void StringReverse(char * pInputStr, char * pOutputStr)
{
   int i,j=0;
   for(i=strlen(pInputStr)-1;i!=-1;i--)
     {
       if(pInputStr[i]>='A'&&pInputStr[i]<='Z')
            pOutputStr[j++]=pInputStr[i];
      }
}

7.题目描述:输入一段英文文本,用程序统计出现频率最高和最低的两个单词;英文文本中仅出现这四类字符:空格( )、英文逗号(,)、英文句号(.)、英文大小写字母(a-z、A-Z)单词之间的分隔符仅考虑这三种:空格( )、英文逗号(,)、英文句号(.);仅大小写不同的单词算同一个单词;如果两个单词出现次数相同,则在文本中首次出现的单词优先返回。返回的单词统一用小写字母返回。

要求实现函数: 
void WordStat(const char * pInputStr, char * pOutputHotWord, char * pOutputColdWord);

【输入】pInputStr:输入字符串,指向一段英文文本
【输出】pOutputHotWord: 输出字符串,返回出现次数最多的单词,该指针所指存储空间已经分配好
        pOutputColdWord:输出字符串,返回出现次数最少的单词,该指针所指存储空间已经分配好
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
 示例 :
       输入字符串“Hello world, i said hello world to the world”,返回“world”,“i”
       输入字符串“Somebody like somebody,i do not like it”,返回“somebody”,“i”

void WordStat(const char * pInputStr, char * pOutputHotWord, char * pOutputColdWord)
{
    map<string,vector<int> >word_count;
    int i=0;

    string strWord;

    vector<int>ivec;

    while(i<strlen(pInputStr))

     {

       while(isalnum(pInputStr[i]))

           strWord+=tolower(pInputStr[i++]);
       if(strWord!="")
        {
           ivec.push_back(1);
           ivec.push_back(i);
           pair<map<string,vector<int> >::iterator,bool> ret

           =word_count.insert(make_pair(strWord,ivec));
           if(!ret.second)
           ++ret.first->second[0];
           strWord="";
           ivec.clear();
         }
       i++;
      }

    map<string,vector<int> >::iterator map_it=word_count.begin();
    map<string,vector<int> >::iterator map_it_hot=word_count.begin();
    map<string,vector<int> >::iterator map_it_cold=word_count.begin();

    while(map_it!=word_count.end())

      {
        if((map_it->second[0])>(map_it_hot->second[0])||(map_it->second[0])==

           (map_it_hot->second[0])&&(map_it->second[1])<(map_it_hot->second[1]))

              map_it_hot=map_it;
        if((map_it->second[0])<(map_it_cold->second[0])||(map_it->second[0])==

           (map_it_cold->second[0])&&(map_it->second[1])<(map_it_cold->second[1]))
              map_it_cold=map_it;
        map_it++;
       }

    for(i=0;i<strlen(map_it_hot->first.c_str());i++)
        pOutputHotWord[i]=map_it_hot->first[i];
    for(i=0;i<strlen(map_it_cold->first.c_str());i++)
        pOutputColdWord[i]=map_it_cold->first[i];
}

8.题目描述:对于一个字符串,判断其中是否包含长度>=2的相同子串,如果包含,返回1,不包含,返回0。

 要求实现函数:int sameStr(char *pInputStr);

【输入】 pInputStr:  输入字符串 

【返回】若包含长度>=2的相同子串返回1,否则返回0

int sameStr(char *pInputStr)
{
    char buffer[MAXCHAR]={0};
    if(strlen(pInputStr)>=4)
     {
       for(int i=0;i<strlen(pInputStr)-4;i++)
        {
          strncpy(buffer,pInputStr+i,2);
          if(strstr(pInputStr+i+2,buffer)!=NULL)
             return 1;
          }
     }
    return 0;
}

9.描述:编写一个字符串替换函数,如函数名为 StrReplace(char* strSrc,char* strFind, char* strReplace),strSrc为原字符串,strFind是待替换的字符串,strReplace为替换字符串。举个直观的例子吧,如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”这个字符串,把其中的“RST”替换为“ggg”这个字符串,结果就变成了:ABCDEFGHIJKLMNOPQgggUVWXYZ

void StrReplace(char* strSrc, char* strFind, char* strReplace)
{
        int i,j;
        while(strstr(strSrc,strFind)!=NULL)
          {  
             i=strstr(strSrc,strFind)-strSrc;
             for(j=0;j<strlen(strFind);j++)
             strSrc[i+j]=strReplace[j];
           }

}

10.描述:给定一个整型数,逆序输出整形数,如果有重复的数字,只保留一个,若结尾是0则忽略,保留符号。

例如:Input:12223        Output:321

      Input:-23330300    Output:-302

void ReverseZipInt(int inputInt, int& outputInt)
{
     char Output1[MAXCHAR]={0};
     char output2[MAXCHAR]={0};
     itoa(inputInt,Output1,10);
     int a[10]={0};
     int i,j=0,pos;
     i=strlen(Output1)-1;
     while(Output1[i]=='0')
        i--;
     for(;i!=-1;i--)
      {
         pos=Output1[i]-'0';
         if(a[pos]==0)
         {
            output2[j++]=Output1[i];
             a[pos]++;
          }
       }
     outputInt=atoi(output2);
     if(inputInt<0)
        outputInt=-outputInt;
}

11.问题描述: 输入一个只包含个位数字的简单四则运算表达式字符串,计算该表达式的值

注:1、表达式只含 +, -, *, / 四则运算符,不含括号
    2、表达式数值只包含个位整数(0-9),且不会出现0作为除数的情况
    3、要考虑加减乘除按通常四则运算规定的计算优先级
    4、除法用整数除法,即仅保留除法运算结果的整数部分。比如8/3=2。输入表达式保证无0作为除数情况发生
    5、输入字符串一定是符合题意合法的表达式,其中只包括数字字符和四则运算符字符,除此之外不含其它任何字符,不会出现计算溢出情况

要求实现函数: int calculate(int len,char *expStr)
【输入】 int len: 字符串长度;
         char *expStr: 表达式字符串;
【输出】 无
【返回】 计算结果
示例 
1) 输入:"1+4*5-8/3"   函数返回:19
2) 输入:"8/3*3"       函数返回:6 

int calculate(int len,char *expStr)
{
    assert(expStr!="");
    char *tempStr=new char[len];
    int i,j=0;
    int opA,opB,opResult;
    for(i=0;i<len;i++)
    {
       if(expStr[i]!='*'&&expStr[i]!='/')
             tempStr[j++]=expStr[i];
       else
          {
             opA=expStr[i-1]-'0';
             opB=expStr[i+1]-'0';
             if(expStr[i]=='*')
                 opResult=opA*opB;
             else
                 opResult=opA/opB;
             itoa(opResult,tempStr+j-1,10);
             j=strlen(tempStr);
             i++;
           }
      }

   int result=0;

   bool flag=true;

   char ch;
   char *tempOperand=new char[len];
   i=0,j=0;
   while(i<strlen(tempStr))
    {
      if(tempStr[i]=='-'||tempStr[i]=='+')
          ch=tempStr[i++];
      else
        {
          while(isdigit(tempStr[i]))
          tempOperand[j++]=tempStr[i++];
          if(flag==true)
           {
              result+=atoi(tempOperand);
              flag=false;
              memset(tempOperand,0,strlen(tempOperand));
             }
          else
           {
              if(ch=='+')
               {
                  result+=atoi(tempOperand);
                  memset(tempOperand,0,strlen(tempOperand));
                }
              else
               {
                  result-=atoi(tempOperand);
                  memset(tempOperand,0,strlen(tempOperand));
                }
             }
          j=0;
         }  
     }
   return result;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值