// 01.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <vector>
using namespace std;
int main()
{
//string word;
遇到文件结束标记或者非法输入循环终止
//while (cin >> word)
//{
// cout << word << endl;
//}
//string line;
//
//while (getline(cin,line))
//{
// //if(!line.empty())
// if(line.size()>10)
// cout << line << endl;
//
//}
//string s2 = "world";
//string s1 = "hello" + s2;
运算符两侧的运算对象至少有一个是string
//cout << s1 << endl;
/* std::string line;
while (std::getline(std::cin, line)) {
std::istringstream iss(line);
std::string word;
while (iss >> word) {
std::cout << word << std::endl;
}
}*/
//练习3.3
/*string s1, s2;
cin >> s1 >> s2;
if (s1 == s2)
{
cout << "s1 == s2" << endl;
}
else
{
if (s1 > s2)
{
cout << s1 << endl;
}
else
{
cout << s2 << endl;
}
}*/
/*string s1, s2;
cin >> s1 >> s2;
if (s1.size() == s2.size())
{
cout << "s1 == s2" << endl;
}
else
{
if (s1.size() > s2.size())
{
cout << s1 << endl;
}
else
{
cout << s2 << endl;
}
}*/
/*string s1, s2,s3;
cin >> s1 >> s2 >> s3;
string s4 = s1 +" "+ s2+" " + s3;
cout << s4 << endl;*/
//处理每个字符,使用基于范围的for语句
//一 使用for语句把string对象中的字符每行一个输出出来吧
/*string str("some string");
for (auto c : str)
{
cout << c << endl;
}*/
// 二 使用for语句和ispunct函数来统计string对象中标点符号的个数
//注意包含头文件<cctype>
//string s("Hello World !!!");
//punct_cnt的类型和s.size()的返回类型一样;
/*decltype (s.size()) punct_cnt = 0;
for (auto c : s)
if (ispunct(c))
++punct_cnt;
cout << punct_cnt << " punctuation characters" << s << endl;*/
//使用for语句改变字符串中的字符
//循环变量需要定义成引用类型
//使用标准库函数toupper,该函数接受一个字符,然后输出其对应的大写形式
//string s("Hello World");
转换为大写形式
//for (auto& c : s) //c是引用
// c = toupper(c); //c是引用,因此赋值语句将改变s中的字符
//cout << s << endl;
//只处理一部分字符,将首字符改写成大写形式
//string 对象的下标必须大于0而小于s.size();
/*string s("some string");
if (!s.empty())
s[0] = toupper(s[0]);
cout << s << endl;*/
//将s的第一个单词改写成大写形式
/*string s("some string");
for (decltype(s.size()) index = 0;
index != s.size() && !isspace(s[index]); ++index)
s[index] = toupper(s[index]);
cout << s << endl;*/
return 0;
}
string(2)个人笔记
于 2023-11-04 11:47:17 首次发布