💞💞 前言
hello hello~ ,这里是大耳朵土土垚~💖💖 ,欢迎大家点赞🥳🥳关注💥💥收藏🌹🌹🌹
💥个人主页:大耳朵土土垚的博客
💥 所属专栏:C++入门至进阶
这里将会不定期更新有关C++的内容,希望大家多多点赞关注收藏💖💖
💥本篇博客将使用string类求解五个题目,包括题目链接,解题思路以及实现代码,题目中有关函数的理解和使用,也会挑出一些来介绍🥳🥳
目录
- 💞💞 前言
- ☑️找出字符串中第一个只出现一次的字符
- ☑️字符串里面最后一个单词的长度
- ☑️翻转字符串
- ☑️字符串相加
- ☑️字符串转整形数字
- 结语
☑️找出字符串中第一个只出现一次的字符
✨✨题目链接点击跳转
解题思路:
这里可以参考我们之前学习过的计数排序:
①先定义一个int数组,大小为26*sizeof(int),用来一一对应26个字母(小写),记录每个字母出现的次数;
②然后遍历题目中的字符串s,计算出每个字母的个数存放在之前定义的数组中;
③最后再通过遍历原字符串s找出第一次只出现一次的字符。
代码如下:
#include using namespace std; #include int main() { string s; //原字符串 cin>>s; int count[26] = {0} ; //开辟一个数组计数 for(auto ch : s) //范围for遍历 { count[ch-'a']++; //计数 } for(auto ch : s) //再次遍历找到第一个出现一次的字符 { if(count[ch-'a'] == 1) { cout string s; getline(cin,s); //获取一行字符串 int pos = s.rfind(' ');//从后往前遍历找到空格的位置 int length = s.size() - pos-1; //计算最后一个单词长度 cout std::string line; std::cout std::string str = "Hello World, hello C++"; std::size_t found = str.rfind("hello"); if (found != std::string::npos) { std::cout std::cout public: void reverseString(vector //定义左右下标 int left = 0; int right = s.size()-1; char tmp = '0'; while(left