【2012年华为校园招聘软开上机-成都】字母转换、统计单词个数

2013-09-11 15:18:51

1 字串转换

问题描述:

将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a;若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。例如:aa 转换为 bc,zz 转换为 ab;当连续相同字母超过两个时,第三个出现的字母按第一次出现算。

要求实现函数:

int convert(char *input,char* output)
【输入】  char *input , 输入的字符串
【输出】  char *output ,输出的字符串
【返回】 无

示例

输入:char*input="abcd" 
输出:char*output="bcde"
输入:char*input="abbbcd" 
输出:char*output="bcdcde"

2 字符串处理转换

问题描述:    

在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单词后不加空格。

要求实现函数:

void my_word(charinput[], char output[])
【输入】  char input[], 输入的字符串
【输出】  char output[],输出的字符串
【返回】 无


示例

输入:charinput[]="some local buses, some1234123drivers" ,
输出:charoutput[]="drivers local buses some"
输入:charinput[]="%A^123 t 3453i*()" ,
输出:charoutput[]=""


代码:

  1 #include<iostream>
  2 #include <cassert>
  3 #include <map>
  4 #include<string>
  5 using namespace std;
  6 
  7 typedef char DataType;
  8 
  9 const size_t SIZE = 100;
 10 
 11 void StringConvert(DataType *pInputStr,DataType *pOutputStr)
 12 {
 13     assert(pInputStr != NULL && pOutputStr != NULL);
 14 
 15     DataType *pInCur = pInputStr;
 16     //DataType *pInPre = "";
 17     DataType CharInPre = '';
 18     DataType *pOutCur = pOutputStr;
 19     size_t increment = 0;
 20 
 21     while (*pInCur)
 22     {
 23         assert(*pInCur >= 'a' && *pInCur <= 'z');
 24 
 25         if (*pInCur == CharInPre)
 26         {
 27             increment = 2;
 28             //pInPre = '';  //用指针将会改变源输入字符串,因此用CharInPre
 29             CharInPre = '';  //将CharInPre归零,使得出现2次以上的字符俺第一次出现处理
 30         }
 31         else
 32         {
 33             increment = 1;
 34             CharInPre = *pInCur;
 35         }
 36 
 37         //*pOutCur = 'a' + (*pInCur - 'a' + increment) % ('z' - 'a');
 38         *pOutCur++ = 'a' + (*pInCur - 'a' + increment) % 26;
 39         ++pInCur;
 40     }
 41 }
 42 
 43 bool IsAlphabet(char ch)
 44 {
 45     return ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));
 46 }
 47 
 48 void CountWord(const string &inputStr,string &outputStr)   //注意此处输出必须使用引用形参
 49 {
 50     multimap<int,string,greater<int>> mapLengthWord;
 51     //map<int,string,greater<int>> mapLengthWord;
 52 
 53     string tmpStr;
 54     size_t tmpLen = 0;
 55     size_t index = 0;
 56 
 57     while (index < inputStr.length() )
 58     {
 59         tmpStr.erase(0,tmpStr.length());  //注意清零
 60 
 61         while (index < inputStr.length())
 62         {
 63             if ( !IsAlphabet(inputStr.at(index)) )
 64             {
 65                 break;
 66             }
 67             tmpStr.push_back(inputStr.at(index));
 68             ++index;
 69         }
 70 
 71         tmpLen = tmpStr.length();
 72 
 73         if ( tmpLen >= 2)
 74         {
 75             mapLengthWord.insert(multimap<int,string>::value_type (tmpLen,tmpStr));
 76         }
 77 
 78         ++index;
 79     }
 80 
 81     multimap<int,string,greater<int>> ::iterator iter;
 82     //map<int,string,greater<int>> ::iterator iter;
 83     //cout<<"mapLengthWord.size() = "<<mapLengthWord.size()<<endl;
 84 
 85     for (iter = mapLengthWord.begin();iter != mapLengthWord.end();++iter)
 86     {
 87         cout<<iter->first<<" "<<outputStr<<endl;
 88         if (iter != mapLengthWord.begin() )   //不支持iter + n的算术操作
 89         {
 90             outputStr.append(" " + iter->second);
 91         }
 92         else
 93         {
 94             outputStr.append(iter->second);
 95         }
 96     }
 97 }
 98 
 99 void TestDriver()
100 {
101     //测试StringConvert
102     //DataType pInputStr[SIZE] = "abcd";
103     //DataType pInputStr[SIZE] = "abbcd";
104     //DataType pInputStr[SIZE] = "abbbcd";
105     /*DataType pInputStr[SIZE] = "";
106     DataType pOutputStr[SIZE] = "";
107     StringConvert(pInputStr,pOutputStr);
108 
109     cout<<"pInputStr = "<<pInputStr<<endl;
110     cout<<"pOutputStr = "<<pOutputStr<<endl;*/
111 
112     //测试CountWord
113     //string inputStr = "some local buses, some1234123drivers";
114     //string inputStr = "sometimes abc de, some1234123here#a@as$drivers";
115     string inputStr = "%A^123 t 3453i*()";
116     string outputStr;
117 
118     CountWord(inputStr,outputStr);
119 
120     cout<<"inputStr = "<<inputStr<<endl;  //要输出string类型的变量,需包含string头文件
121     cout<<"outputStr = "<<outputStr<<endl;
122 }
123 
124 int main()
125 {
126     TestDriver();
127     return 0;
128 }

测试结果:

inputStr = sometimes abc de, some1234123here#a@as$drivers
outputStr = sometimes drivers some here abc de as
请按任意键继续. . .
原文地址:https://www.cnblogs.com/youngforever/p/3314645.html