// string类的操作.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 题目:
//1.编写一个程序检查用户输入的单词是否为回文。例如,ATOYOTA是回文,因为该单词反转后与原来相同。
//2.编写一个程序,告诉用户输入的句子包含多少个元音字母。
//3.将字符串的字符交替地转换为大写。
//4.编写一个程序,将4个string对象分别初始化为I、Love、STL和String,然后在这些字符串之间添加空格,再显示整个句子。
//5.编写一个程序,显示字符串Good day String! Today is beautiful!中每个a所在的位置。
#include <iostream>
#include <string>
using namespace std;
void question1(string str1)
{
string str2 = str1;
reverse(str1.begin(), str1.end());
if (str1.compare(str2) == 0)
{
cout << str2 << "为回文" << endl;
}
else
{
cout << str2 << "不是回文" << endl;
}
}
void question2(string str2)
{
char YanYin[] = "aeiou";
int ShowNumber = 0;
size_t Place = 0;
for (int i = 0; i < sizeof(YanYin); i++)
{
Place = str2.find(YanYin[i], 0);
while (Place != string::npos) // npos意为"no position"——没有位置
{
Place = str2.find(YanYin[i], Place + 1);
ShowNumber++;
}
}
cout << "元音字母的个数为" << ShowNumber << endl;
}
void question3(string str3)
{
string str;
for (int i = 0; i < str3.size(); i++)
{
if (i % 2 == 0)
{
str += toupper(str3[i]);
}
else
{
str += tolower(str3[i]);
}
}
cout << "大小写交替的字符串为" << str << endl;
}
void question4(string str4_1, string str4_2, string str4_3)
{
str4_1.append(" ");
str4_1 += str4_2;
str4_1.append(" ");
str4_1.append(str4_3.begin(), str4_3.end());
cout << str4_1 << endl;
}
void question5(string str5)
{
size_t Place = 0;
int i = 0;
Place = str5.find('a', 0);
while (Place != string::npos)
{
cout << "第\"" << ++i << "个字符'a'\"的位置为" << Place << " ";
Place = str5.find('a', Place + 1);
}
cout << endl;
}
int main()
{
cout << "question5" << endl;
string str5 = "Good day String! Today is beautiful!";
question5(str5);
cout << "question4" << endl;
string str4_1 = "I", str4_2 = "Love", str4_3 = "You";
question4(str4_1, str4_2, str4_3);
cout << "question3" << endl;
string str3("Chinese Happier");
question3(str3);
cout << "question2" << endl;
string str2("Please enter a string for case-conversion:");
question2(str2);
cout << "question1" << endl;
string str1("ATOYOTA");
question1(str1);
}