给定一个字典,通过查找这个字典,替换给定的字符串中的中文为英文
2017-03-17 22:09
357 查看
描述:第一对START和END中给定的是字典
第二对START和END是给定字符串
输入:
START
hello nihao
yes shi
like xihuan
world shijie
gril nvhai
END
START
i'm a nvhai!i xihuan shijie.nihaoEND
输出:
i'm a gril!
i like world.
hello
《完》
第二对START和END是给定字符串
输入:
START
hello nihao
yes shi
like xihuan
world shijie
gril nvhai
END
START
i'm a nvhai!i xihuan shijie.nihaoEND
输出:
i'm a gril!
i like world.
hello
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <vector> #include <map> using namespace std; const string Start = "START"; const string End = "END"; int main() { map<string, string> myMap; string value; while (cin >> value){ if (value == Start) continue; else if (value == End) break; else{ string key; cin >> key; myMap[key] = value; } } cin.get(); string linestr; vector<string> historyBook; while (getline(cin, linestr)){ if (linestr == Start) continue; else if (linestr == End) break; else{ historyBook.push_back(linestr); } } int size = historyBook.size(); int line = 0; while (line < size){ string& curline = historyBook[line]; int strSize = curline.size(); int f = 0, l = 0; while (l<=strSize){ if (l == strSize || curline[l] == ',' || curline[l] == ' ' || curline[l] == '!' || curline[l] == '.'){ //读完一个单词 char curWord[20];//假设每个单词长度不超过20 strncpy(curWord, (char*)&curline[f], l - f); curWord[l - f] = '\0';//把当前读完的单词放入curWord中 string tmpkey = curWord; map<string,string>::iterator it=myMap.find(tmpkey); if (it != myMap.end()){//如果未找到,it==myMap.end() curline.erase(f, l - f);//删除原来的汉语部分 curline.insert(f, it->second);//在原来的位置插入所替换的英文 f = f + (it->second).size()+1; l = f; strSize += ((it->second).size() - (it->first).size());//长度改变,所以要更新 } else{ f = l + 1; l = f; } } else ++l; } ++line; } for (int i = 0; i < historyBook.size(); ++i){ cout << historyBook[i] << endl; } system("pause"); return 0; }
《完》
相关文章推荐
- 给定一个只包含大写英文字母的字符串,可以用另一个字母替换字符串中的任何字母最多k次。查找包含所有重复字母的最长子字符串的长度,
- 给定一个英文字符串,请写一段代码找出这个字符串中首先出现三次的那个字符。
- 给定一个字符串,包含中文字符和英文字符,取给定大小字节的子串。
- 给定一个英文字符串,找出这个字符串中首先出现三次的那个英文字符
- 空格替换 请编写一个方法,将字符串中的空格全部替换为“%20”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成。 给定一
- 请编写一个方法,将字符串中的空格全部替换为“%20”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成。 给定一个string
- 给定一个字符串,包含中文字符和英文字符,取给定大小字节的子串。
- 给你一个单词a,如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的兄弟单词。现在给你一个字典,用户输入一个单词,让你根据字典找出这个单词有多少个兄弟单词。
- 在一个JS文件中包含中文字符串通过innerHTML输出后中文乱码
- 找出自己应用程序里面所有的中文字符串,替换为字符串变量,并将所有字符串放到一个文本文件中。
- 一个中文汉字算两位,一个英文字母算一位,计算中文和英文混合的字符串长度。字符串:String str = "我不好java";
- 回复:String中如果有中文,如何保证length()返回的长度 在 Java 中字符串是以 Unicode 方式编码的,所以,无论中英文都算的长度为 1。根据你的描述,估计是想要取得英文长度为 1 中文长度为 2 的总长度吧。这个,理论上可以构造 An
- [转]给你一个单词a,如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的兄弟单词。现在给你一个字典,用户输入一个单词,让你根据字典找出这个单词有多少个兄弟单词
- 字符串统计,一个中文算2个字符,一个英文算一个字符
- 判断一个给定的字符串通过循环移位是否可以包含另一个字符串
- 百度面试题--给定一个单词,从字典查找该单词的所有兄弟单词
- 设计函数,输入为一个字符串,里边包含中文、英文、数字等字符,编码为GBK。中文字符的编码规则假定为:双字节组成,高字节大于0x80,低字节任意。
- 设计一个程序,从键盘上输入若干字符串,利用算法库中的查找函数对给定的字符串进行查找,将查找后的结果输出
- 3g:如何查找一个字符串中重复次数最多的字符,返回这个字符的个数和字符
- 替换String字符串中的中文括号为英文括号