Reverse Vowels of a String (反转字符串中的母音)

博客围绕LeetCode反转字符串中的母音展开,介绍了学习要点,包括使用find_first_of查找与指定字符串中字符相同的首个位置,若未找到则返回特定值,以及用find_last_of从后往前匹配,还提及了代码和运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

leetcode Reverse Vowels of a String 反转字符串中的母音
一、学习要点:
1.find_first_of:查找与字符串str中某个字符相同的位置,并返回他的第一个出现的位置,如果没有返回string::npos;即最后一个字符的后面一个位置;
2.find_last_of:从后往前匹配;
二、代码:

#include<stdlib.h>
#include<stdio.h>
#include<string>
using namespace std;
class Solution
{
public:
 string reverseVowels(string s)
 {
  int i = 0;
  int j = s.size() - 1;
  string temp = "aoeiuAOEIU";
  while (i < j) {
   i = s.find_first_of(temp, i);
   j = s.find_last_of(temp, j);
   if (i < j) {
    swap(s[i++], s[j--]);
   }
  }
  return s;
 }
};
int main()
{
 string s = "hello";
 string s1;
 Solution ob; 
 s1 = ob.reverseVowels(s);
 for (string::iterator iter = s1.begin(); iter != s1.end(); iter++)
 {
  printf("%c",*iter);
 }
 system("pause");
 return 0;
}

三、运行结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值