118A. String Task
implementation/strings, 1000, http://codeforces.com/problemset/problem/118/A
思路:遇到元音continue,其余情况变成小写然后输出.和该字母,用时较长(处理输出次数太多),但代码较短 ,用时约15min(没注意到y也算元音)
cpp
#include <iostream>
using namespace std;
class solution
{
private:
string str;
public:
bool isVowels(char c)
{
return (c == 'a' || c == 'y'|| c == 'e' || c == 'i' || c == 'o' || c == 'u'
|| c == 'A' || c == 'Y' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
}
bool isUppercase(char c)
{
return (c >= 'A' && c <= 'Z');
}
void getStr()
{
cin >> str;
}
void outPut()
{
for (auto i : str)
{
if (isVowels(i)) continue;
if (isUppercase(i)) i = tolower(i);
cout << '.' << i;
}
}
};
int main()
{
solution ans;
ans.getStr();
ans.outPut();
return 0;
}思路:
遍历字符串,将字母转为小写;
若是元音(a, o, y, e, u, i)则跳过;
否则在结果中添加 '.' 和该字母;
最后输出处理后的字符串。
代码:
cpp
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string s, result;
cin >> s;
for (char c : s) {
c = tolower(c);
if (c == 'a' || c == 'o' || c == 'y' || c == 'e' || c == 'u' || c == 'i')
continue; // 跳过元音
result += '.';
result += c;
}
cout << result << endl;
return 0;
}