You have decided that PGP encryptation is not strong enough for your
email. You have decided to supplement it by first converting your
clear text letter into Pig Latin before encrypting it with PGP.
Input and Output
You are to write a program that will take in an arbitrary number of
lines of text and output it in Pig Latin. Each line of text will
contain one or more words. A “word” is defined as a consecutive
sequence of letters (upper and/or lower case). Words should be
converted to Pig Latin according to the following rules (non-words
should be output exactly as they appear in the input):
- Words that begin with a vowel (a, e, i, o, or u, and the capital versions of these) should just have the string “ay” (not including the
quotes) appended to it. For example, “apple” becomes “appleay”.- Words that begin with a consonant (any letter than is not A, a, E, e, I, i, O, o, U or u) should have the first consonant removed and
appended to the end of the word, and then appending “ay” as well. For
example, “hello” becomes “ellohay”.- Do not change the case of any letter.
Sample Input
This is the input.
Sample Output
hisTay isay hetay inputay.
/*总体思路:
将输入的字符串逐个字符进行判断处理;
*/
#include<iostream>
#include<string>
using namespace std;
//判断是否为字母
int isab(char ch){
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
return 1;
else
return 0;
}
//判断是否为元音字母
int vowel(char ch){
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'
||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
return 1;
else
return 0;
}
int main(){
string str;
getline(cin,str);
char temp;
int flag=0; //flag用于标记某字符是否在单词内
for(int i=0;i<str.size();i++){
//这个字符不是字母,但 flag==1,说明一个单词结束了。
if(!isab(str[i])&&flag==1){
if(isab(temp))
cout<<temp;
cout<<"ay";
cout<<str[i];
temp='@';
flag=0;
}
//不是挨着单词的其他字符,那就直接输出
else if(!isab(str[i]))
cout<<str[i];
//flag==0,是一个元音字母,它是一个元音字母开头的单词
else if(vowel(str[i])&&flag==0){
cout<<str[i];
flag=1;
}
// 同理,但是辅音开头,先存入temp,整个单词的其余字符输出完成后再输出它(第31行)
else if(!vowel(str[i])&&flag==0){
temp=str[i];
flag=1;
}
//单词内部的字符直接输出即可
else
cout<<str[i];
}
cout<<endl;
}