hdu1048(c++)

用string串来处理这个题很方便

#include<iostream>
#include<string>
using namespace std;
string cipher(string &s); //引用为参数,此函数返回一个string类的字符串
int main()
{
 string s1, s2, s3,s4;
 while (1)
 {
  getline(cin, s1);
  if (s1 == "ENDOFINPUT")
   break;
  getline(cin, s2);
  getline(cin, s3);
  s4 = cipher(s2);
  cout <<s4<< endl;
 }
 return 0;
}
string cipher(string &s)
{
 char *p = new char[s.size() + 1];//设置动态字符数组
 p[s.size()] = 0;                          //将数组最后元素设置为0,即为空字符,结束标志
 int i;
 for (i = 0; i < s.size(); i++)
 {
  if (s[i] <= 'E'&&s[i] >= 'A')
   p[i] = s[i] + 21;
  else if ('E' < s[i] && s[i] <= 'Z')
   p[i] = s[i] - 5;
  else
   p[i] = s[i];
 }
 string s1(p); //整赋值
 delete[]p;
 return s1;
}

posted @ 2015-03-01 16:19  曹孟德  阅读(237)  评论(0编辑  收藏  举报