#include <iostream>
#include <string>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
int main() {
//编写一段程序,使用范围for语句将字符串内所有字符用X代替
string word11("today is a gift!");
for (auto& b : word11)
b = 'X';
cout << word11 << endl;
//如果将循环控制变量的类型设置为char将变化为什么
string word12("today is a gift!");
for (char &c : word12)
c = 'X';
cout << word12 << endl;
//用while 循环重写上题
string word13("today is a gift!");
decltype(word13.size()) m=0;
while (m<word13.size() ){
word13[m] = 'X';
cout << word13[m];
m++;
}
cout << '\n';
//编写一段程序,读入一个包含标点符号的字符串,将标点符号除去后输出字符串剩余部分
string I_string;
decltype(I_string.size()) p=0;
cout << "请输入一个包含标点符号的字符串,然后按enter键:" << endl;
cin >> I_string;
for (auto& b : I_string)
if (!ispunct(b))
putchar(b);
cout << '\n';
//*****处理每个字符,使用基于范围的for语句
string str1("some string");
//每行输出str中的一个字符
for (auto c : str1) //对于str中的每一个字符
cout << c << endl; //输出当前字符,后面紧跟一个换行符
string s("hello world! 0x112");
decltype(s.size()) punct_cnt = 0; //把punct_cnt的类型设为与s.size()相同的类型
for (auto d : s)
if (ispunct(d))
++punct_cnt;
cout << punct_cnt << " punctuation characters in " << s << endl;
int number=0;
for (auto e : s)
if (isalnum(e))
number++;
cout << "总共有" << number << "个字母和数字在" << s << "中" << endl;
int number1 = 0;
for (auto b : s)
if (isalpha(b))
number1++;
cout << "共有" << number1 << "个字母在" << s << "中" << endl;
int number2 = 0;
for (auto b : s)
if (iscntrl(b))
number2++;
cout<<"共有"<<number2<<"个控制字符在"<<s<< "中" << endl;
int number3 = 0;
for (auto c : s)
if (isdigit(c))
number3++;
cout << "共有" << number3 << "个数字在" << s << "中" << endl;
int number4 = 0;
for (auto b : s)
if (isgraph(b))
number4++;
cout << "不是空格但可以打印的字符个数有" << number4 << endl;
int number5 = 0;
for (auto b : s)
if (islower(b))
number5++;
cout << "小写字母共有" << number5 << "个"<<endl;
int number6 = 0;
for (auto b : s)
if (isprint(b))
number6++;
cout << "可打印的字符个数为" << number6 << endl;
int number7 = 0;
for (auto b : s)
if (ispunct(b))
number7++;
cout << "标点符号的个数为" << number7 << endl;
int number8 = 0;
for (auto b : s)
if (isspace(b))
number8++;
cout << "空白处的个数为" << number8 << endl;
int number9 = 0;
for (auto b : s)
if (isupper(b))
number9++;
cout << "大写字母的个数为" << number9 << endl;
int number10 = 0;
for (auto b : s)
if (isxdigit(b))
number10++;
cout << "十六进制数字的个数为" << number10 << endl;
for (auto b : s)
putchar(tolower(b));
//tolower(c)如果c是大写字母,输出对应的小写字母,否则原样输出,注意转换后为int类型,需要显式转换为char
for (auto b : s)
putchar(toupper(b));
//toupper(b) 如果b是小写字母,输出对应的大写字母,否则原样输出,注意转换后为int类型,需要显式转换为char
//使用for语句改变字符串中的字符
for (auto& b : s)
b = toupper(b);
cout << s << endl;
//好神奇,用引用就不需要显式转换
//要想访问string对象中的单个字符有两种方式:一种是使用下标,另一种是使用迭代器
//下标运算符[] 接收的输入参数是string::size_type来行的值
//s[0]是第一个字符、s[1]是第二个字符、s[s.size()-1]是最后一个字符
//string对象的下标必须大于等于0小于s.size()
//下标的值称作“下标”或“索引”,任何表达式只要它的值是一个整型值就能作为索引
//如果某个索引是带符号类型的值将自动转换称有string::size_type表达的无符号类型
if (!s.empty())
cout << s[0] << endl;
if (!s.empty())
s[0] = tolower(s[0]);
cout << s << endl;
for (decltype(s.size())index = 0; index != s.size() && !isspace(s[index]); ++index)
s[index] = tolower(s[index]);
cout << s << endl;
//************使用下标执行随机访问*******************/
const string hexdigits = "0123456789ABCDEF";
cout << "Enter a series of numbers between 0 and 15"
<< "separated by space.Hit ENTER when finished: "
<< endl;
string result; //用于保存16进制的字符串
string::size_type n; //用于保存输入流读取的数
while (cin >> n)
if (n < hexdigits.size())
result += hexdigits[n]; //得到对应的16进制数字
cout << "Your hex number is : " << result << endl;
/*********无论何时用到字符串的下标都要注意其合法性,用string::size_type类型这样的无符号类型/
/*******本例中还需检查n的长度是否小于hexdigits的长度*/
//输入多个字符串并将它们连接在一起,输出连接成的大字符串
string word8,big1;
while (cin >> word8) {
big1 += word8;
}
cout << big1 << endl;
cin.clear();
//输入多个字符串,用空格把输入的多个字符串分隔开来,并输出连接成的大字符串
string word9, big2;
while (cin >> word9) {
big2 += word9 + " ";
}
cout << big2 << endl;
cin.clear();
//读入两个字符串,比较其是否相等并输出结果,如果不相等,输出较大的那个字符串
string word4, word5;
while (cin >> word4 ) {
cin >> word5;
if (word4 > word5)
cout << word4 << endl;
else {
cout << word5 << endl;
}
}
cin.clear();
//读入两个字符串,比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串
string word6, word7;
while (cin >> word6) {
cin >> word7;
if (word6.size() > word7.size())
cout << word6 << endl;
else {
cout << word7 << endl;
}
}
cin.clear();
//一次读入一行
string line2;
while ( getline (cin, line2)) {
cout << line2 << endl;
}
cin.clear();
//一次读入一个词
string word3;
while (cin >> word3) {
cout << word3 << endl;
}
string s1;
string s2 = s1;
string s3 = "hello";
string s4(10, 'c');
cout << s1 << s2 << s3 << s4 << endl;
//使用等号的初始化实际上是拷贝初始化copy initialization
//如果不使用等号,则执行的是直接初始化
//当初始值只有一个时,使用直接初始化或拷贝初始化都行
//如果初始化要用到的值有多个,一般来说只能使用直接初始化的方式
string s5("hello");
string s6(10, 'a');
string s7 = string(9, 'b');
//string对象会自动忽略开头的空白(即空格符、换行符、制表符等)
//并从第一个真正的字符开始读起,知道遇见下一处空白为止
cin >> s1 >> s2;
cout << s1 << s2 << endl;
string word;
while (cin >> word) {
cout << word << endl;
}
cin.clear();
//getline函数的参数是一个输入流和一个string对象,函数从给定的输入六中读入内容,
//直到遇到换行符为止(注意换行符也被读进来了),
//然后把所读的内容存入到那个string对象中去(注意不存换行符)
//getline只要一遇到换行符就结束读取操作并返回结果,哪怕一开始输入的就是换行符也一样。
//如果一开始输入的就是换行符,那么所得的结果是一个空string
string line;
while (getline(cin,line))
cout << line << endl;
cin.clear();
//每次读入一整行,遇到空行直接跳过
while (getline(cin, line))
if (!line.empty())
cout << line << endl;
cin.clear();
//size函数返回string对象的长度(即string对象中字符的个数)
while (getline(cin, line))
if (line.size() > 30)
cout << line << endl;
cin.clear();
//size函数返回的是一个无符号整型数,注意不要与带符号混用
//所有用于存放string类的size函数返回值的变量,都应该是string::size_type类型的
/******如果一条表达式中已经有了size()函数就不要再使用int了
这样可以避免混用int和unsigned可能带来的问题***/
//比较string对象的大小
//1.如果两个string对象的长度不同,而且较短string对象的每个字符都与较长string对象对应位置上
//的字符相同,就说较短string对象小于较长string对象
//2.如果两个string对象在某些对应的位置上不一致,则string对象比较的结果其实是string对象中
//第一对相异字符比较的结果(依照字典顺序 大小写敏感)
string str = "Hello";
string phrase = "Hello World";
string slang = "Hiya";
if (str > phrase)
cout << str << endl;
else {
cout << phrase << endl;
}
if (str > slang)
cout << str << endl;
else{
cout << slang << endl;
}
if (phrase > slang)
cout << phrase << endl;
else {
cout << slang << endl;
}
//两个string对象相加 可以用+号,也可以用+=复合运算符
string s8 = "hello, ", s9 = "world!\n";
string s10 = s8 + s9;
cout << s10 << endl;
s8 += s9;
cout << s8 << endl;
//字面值和string对象相加
//当把string对象和字符字面值及字符串字面值混在一条语句中使用时,
//必须确保每个加法运算符+的两侧的运算对象至少有一个是string
string s11 = s1 + 'a';
string s12 = "hello" + s3;
string s13 = "hello" +s2+"world"+"else";
//相当于string s13=(("hello"+s2)+"world")+"else"
//"hello"+string 的结果是一个string
/*************注意:字符串字面值与string是不同的类型***********/
//处理string对象中的字符
//可以使用cctype头文件中的函数
return 0;
}