【C++】STL容器——string类的例题应用(9)

前言

大家好吖,欢迎来到 YY 滴C++系列 ,热烈欢迎!本章主要内容面向接触过C++的老铁,下面是收纳的一些例题与解析~
主要内容含:

欢迎订阅 YY滴C++专栏!更多干货持续更新!以下是传送门!

【例1]给一个字符串(包含字母与数字),将字符串翻转【访问string的size与对象】

  • 如给定“123asd222zxc”,反转后得到“123cxz222dsa”
    在这里插入图片描述
class Solution {
public:
   bool isLetter(char ch)
   {
   if(ch >= 'a' && ch <= 'z')
   return true;
   if(ch >= 'A' && ch <= 'Z')
   return true;
   return false;
   }
   
   string reverseOnlyLetters(string S) 
   {
   if(S.empty())
   return S;
 
  size_t begin = 0, end = S.size()-1;
   while(begin < end)
   {
     while(begin < end && !isLetter(S[begin]))
     ++begin;

     while(begin < end && !isLetter(S[end]))
     --end;
 
     swap(S[begin], S[end]);
     ++begin;
     --end;
   }
   return S;
   }

}

【例2]验证一个字符串是否是回文【范围for遍历字符串】

class Solution {
public:
   bool isLetterOrNumber(char ch)//是否是字母
  {
   return (ch >= '0' && ch <= '9')
   || (ch >= 'a' && ch <= 'z')
   || (ch >= 'A' && ch <= 'Z');
   }

   bool isPalindrome(string s) 
   {
     for(auto& ch : s)//范围for,遍历字符串
     {
     if(ch >= 'a' && ch <= 'z')// 先小写字母转换成大写,再进行判断
     ch -= 32;
     }

   int begin = 0, end = s.size()-1;
   while(begin < end)
    {
       while(begin < end && !isLetterOrNumber(s[begin]))
       ++begin;

       while(begin < end && !isLetterOrNumber(s[end]))
       --end;
  
       if(s[begin] != s[end])
       {
          return false;
       }
        else
       {
         ++begin;
         --end;
       }
    }

   return true;
    }
};

【例3]找字符串中第一个只出现一次的字符【计数】

class Solution {
public:
   int firstUniqChar(string s)
  {

   // 统计每个字符出现的次数
   int count[256] = {0};
   int size = s.size();
   for(int i = 0; i < size; ++i)
   count[s[i]] += 1;

   // 按照字符次序从前往后找只出现一次的字符
   for(int i = 0; i < size; ++i)
   if(1 == count[s[i]])
   return i;

   return -1;
   }
};

【例4]输入一个字符串,求字符串里面最后一个单词的长度【getline函数的应用】

  • getline函数有两种不同的形式,这也就对应着字符串的 结束方式
  • getline函数原型:getline(std::cin,string ch),表示以换行符 ‘\n’ 结束字符串的读入
  • getline函数原型 getline(std::cin,string s,char ch),表示以字符ch来结束字符串的读入
  • 功能: 无视空格,读取输入屏幕的字符串
#include<iostream>
#include<string>
using namespace std;
int main()
{
 string line;
 // 不要使用cin>>line,因为会它遇到空格就结束了
 // while(cin>>line)
 while(getline(cin, line))
 {
 size_t pos = line.rfind(' ');
 cout<<line.size()-pos-1<<endl;
 }
 return 0;
}

【例5】将两个字符串相加(ASCALL码相加)【reverse函数,+=,insert】

  • 题目如下所示:
    在这里插入图片描述
  1. 两个字符串相加的规定是,ASCALL码值相加
  2. 开始前,我们要清楚明白,该题目需要遍历两个字符串
  3. 有两种思路: 从前往后相加,+=尾插以后再reverse过来(代码片所用方法) 或者从后往前相加,相加的结果到字符串可以使用insert头插
  4. 我们先考虑分别遍历一位的情况,后面要采用while循环
  5. (1)遍历字符串的同时,分别取数,将字符的ASCALL通过-“0”,转换成整型
  6. (2)取数完的同时分别相加,考虑进位
  7. (3)最后再+“0”,重新转换成字符,形成字符串
class Solution {
public:
 string addstrings(string num1, string num2)
{
 int end1 = num1.size()-1;
 int end2 = num2.size()-1;
 
 int value1 = 0, value2 = 0, next = 0;//核心设置:
 
 string addret;//形成的新串存放在其中
 
 while(end1 >= 0 || end2 >= 0)
 {
   if(end1 >= 0)
   value1 = num1[end1--]-'0';
   else
   value1 = 0;
 
   if(end2 >= 0)
   value2 = num2[end2--]-'0';
   else
   value2 = 0;

  int valueret = value1 + value2 + next;//next为进位,value1,value2为单次循环中分别的取数
  
  if(valueret > 9)
  {
  next = 1;
  valueret -= 10;
  }
  else
  {
  next = 0;
  }
 //addret.insert(addret.begin(), valueret+'0');
 addret += (valueret+'0');
 }

 // 从后往前相加,相加的结果到字符串可以使用insert头插
 // 或者+=尾插以后再reverse过来
 if(next == 1)
 {
 //addret.insert(addret.begin(), '1');
 addret += '1';
 }

 reverse(addret.begin(), addret.end());
 return addret;
 }
};
  • 45
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 38
    评论
评论 38
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YY的秘密代码小屋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值