一道典型的字符处理题,只要好好读题目就能做出来了。
题目:
Vitya has just started learning Berlanese language. It is known that Berlanese uses the Latin alphabet. Vowel letters are “a”, “o”, “u”, “i”, and “e”. Other letters are consonant.
In Berlanese, there has to be a vowel after every consonant, but there can be any letter after any vowel. The only exception is a consonant “n”; after this letter, there can be any letter (not only a vowel) or there can be no letter at all. For example, the words “harakiri”, “yupie”, “man”, and “nbo” are Berlanese while the words “horse”, “king”, “my”, and “nz” are not.
Help Vitya find out if a word s is Berlanese.
输入:
The first line of the input contains the string s consisting of |s| (1≤|s|≤100) lowercase Latin letters.
输出:
Print “YES” (without quotes) if there is a vowel after every consonant except “n”, otherwise print “NO”.
You can print each letter in any case (upper or lower).
范例:
输入:
sumimasen
输出:
YES
输入:
ninja
输出:
YES
输入:
codeforces
输出:
NO
思路:
我们注意到这一题有一个特殊的字母n,它的判定和元音字母几乎一样,但是有一点很重要:
他不能作为元音字母跟在辅音字母后面!!!
一般来说注意到这点就没什么问题了。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char a[101];
while (cin.getline(a,101))//输入
{
bool err = false;
int len = strlen(a);
for (int i = 0; i < len; i++)
{
if(i!=len-1)//最后一位前面的字符才有下一位,所以分开判断
switch (a[i])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'n':
break;
default:
if (a[i + 1] != 'a' && a[i + 1] != 'e' && a[i + 1] != 'i' && a[i + 1] != 'o' && a[i + 1] != 'u')
err = true;//辅音(除了n)后必跟元音字母
break;
}
else switch (a[i])//最后一位,以n以外的辅音字母结尾绝对是非法的
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'n':
break;
default:
err = true;
break;
}
}
if (err)
cout << "NO" << endl;
else cout << "YES" << endl;//输出
memset(a, 0, sizeof(a));
}
}