如何从字符串中删除辅音?

How to remove consonants from a string?

本文关键字:删除 字符串      更新时间:2023-10-16

这是我的代码:

#include <iostream>
#include <string>
#include<bits/stdc++.h>
using namespace std;
int main() 
{
int n;
cin >> n;
while(n--)
{
string str;
char a[] = {'a','e','i','o','u','A','E','I','O','U'};
getline(cin, str);
for(int i=0 ;i<str.length(); i++)
{
for(int j=0; j<10; j++)
{
if(str[i]==a[j])
{
cout << str[i];
}
}
}
cout << "n"; 
}
return 0;
}

测试用例是:

HmlMqPhBfaVokhR 
wdTSFuI 
IvfHOSNv 

我没有删除任何东西,但我只打印元音。但是,一些测试用例没有通过。也许此代码不适用于多个测试用例。

尝试此操作以获得正确的控制台

int main()
{
int n;
std::cin >> n;
std::cin.ignore();   // fix
/* remaining code */
return 0;
}

> 查找字符串中的元音

在字符串中查找元音的方法是在元音表中使用给定字符串的每个字符std::binary_search

  1. 制作一个所有元音chars 的排序数组(即元音数组(。
  2. 对于输入字符串的每个charstd::binary_search元音数组
  3. 如果std::binary_search返回true(表示char是元音(,请打印字符串的char

下面是示例代码!(在线观看直播(

#include <iostream>
#include <string>
#include <algorithm> // std::for_each, std::binary_search, std::sort
#include <array>     // std::array
int main()
{
std::array<char, 10> a{ 'a','e','i','o','u','A','E','I','O','U' };
std::sort(a.begin(), a.end()); // need sorted array for std::binary_search
const std::string str{ "HmlMqPhBfaVokhR wdTSFuI IvfHOSNv" };
std::for_each(str.cbegin(), str.cend(), [&](const char str_char)
{
if (std::binary_search(a.cbegin(), a.cend(), str_char))
std::cout << str_char << " ";
});
return 0;
}

输出

a o u I I O

> 从字符串中删除元音

使用擦除-删除成语如下(直到c ++ 17(。

  1. 制作一个所有元音chars 的排序数组(即元音数组(。
  2. 使用std::remove_if,收集指向字符的迭代器,这些字符是元音。lambda函数可以用作std::remove_if的谓词,其中std::binary_search用于检查元音数组中是否存在字符串中的char
  3. 使用std::string::erase,从字符串中删除所有收集的字符(即元音(。

下面是一个示例代码!(在线观看直播(

#include <iostream>
#include <string>
#include <algorithm> // std::sort, std::binary_search, std::remove_if
#include <array>     // std::array
int main()
{
std::array<char, 10> a{ 'a','e','i','o','u','A','E','I','O','U' };
std::sort(a.begin(), a.end()); // need sorted array for std::binary_search
std::string str{ "Hello World" };
// lambda(predicate) to check the `char` in the string exist in vowels array
const auto predicate = [&a](const char str_char) -> bool { 
return std::binary_search(a.cbegin(), a.cend(), str_char);
};
// collect the vowels
const auto vowelsToRemove = std::remove_if(str.begin(), str.end(), predicate);
// erase the collected vowels using std::string::erase
str.erase(vowelsToRemove, str.end());
std::cout << str << "n";
return 0;
}

输出

Hll Wrld

从 c++20 开始,可以使用std::erase_if来执行此操作,这比上面的更不容易出错(使用GCC 9.2在线查看(

#include <iostream>
#include <string>    // std::string, std::erase_if
#include <array>     // std::array
int main()
{
std::array<char, 10> a{ 'a','e','i','o','u','A','E','I','O','U' };
std::sort(a.begin(), a.end()); // need sorted array for std::binary_search
std::string str{ "Hello World" };
// lambda(predicate) to check the `char` in the string exist in vowels array
const auto predicate = [&a](const char str_char) -> bool { 
return std::binary_search(a.cbegin(), a.cend(), str_char);
};
std::erase_if(str, predicate); // simply erase
std::cout << str << "n";
return 0;
}

> 从字符串中删除辅音

要从给定的字符串中删除辅音,在上面的predicate否定std::binary_search的结果。(在线观看直播(

const auto predicate = [&a](const char str_char) -> bool { 
return !std::binary_search(a.cbegin(), a.cend(), str_char);
//     ^^ --> negate the return
};

作为旁注,

  • 避免#include<bits/stdc++.h>阅读更多:为什么我不应该 #include ?

  • 不要和using namespace std;一起练习 阅读更多:为什么"使用命名空间 std;"被认为是不良做法?

除了已经回答std::getline问题:

for(int i=0 ;i<str.length(); i++)
{
for(int j=0; j<10; j++)
{
if(str[i] == a[j])
{
// this is the one you do NOT want to print...
// cout<<str[i];
// skip instead:
goto SKIP;
}
}
std::cout << str[i]; // output the one NOT skipped...
SKIP: (void)0;
}

好的,不想开始任何关于goto用法的讨论,有很多方法可以避免它,例如通过将内部 for 循环打包到一个单独的(内联(函数中。不过,您可以更轻松地使用它,因为已经存在这样的函数;使用基于范围的 for 循环,代码变得更加容易:

for(auto c : str)
{
if(!strchr("aeiouAEIOU", c))
{
std::cout << c;
}
}

strchr(来自 cstring(返回指向字符串中第一个字符的指针,该字符等于引用字符 - 如果未找到,则返回 nullptr ...

要以现代C++方式真正从字符串中删除元音,请考虑以下情况:

str.erase(std::remove_if(
str.begin(), str.end(),
[](char c) { return strchr("aeiouAEIOU", c) != nullptr; }
), str.end());

你的代码可能看起来像这样(请参阅内联注释(:

#include <iostream>
#include <string>
using namespace std;
int main() {
string vowels = "aeiouAEIOU";
int n;
cin>>n; // assume this stands for line count
while(n-- >= 0)
{
string str, result;
getline(cin, str);
for(int i=0 ;i<str.length(); i++)
{
if (vowels.find(str[i]) != std::string::npos)
result += str[i];   // add character to result if it is not consonant
}
cout<<result<<"n"; // print result
}
return 0;
}