联发科笔试题之字符编码

题目截图:

直接上代码,没时间啦,明天就笔试啦:

#include <iostream>
#include <string>
using namespace std;
string alphabet = "abcdefghijklmnopqrstuvwxyz";
char replace_char(char s)
{
  for (size_t i = 0; i < alphabet.size();++i)
  {
	  if(s == alphabet[i])
         return alphabet[(i+3)%26]; 
  }
  return s;
}
void coding(string &s)
{
	size_t num = s.size();
	for (size_t i = 0; i < num; ++i)
	{
		if (s[i]>='a' && s[i] <='z')
		{
			s[i] = replace_char(s[i]);
		}
	}
}

int main()
{
	string s;
	cin >> s;
	coding(s);
	cout << s;
}

测试结果:

posted @ 2011-08-11 11:19  hailong  阅读(842)  评论(3编辑  收藏  举报