如何从字符串中删除辅音? [英] How to remove consonants from a string?
问题描述
这是我的代码:
#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
我什么都没删除,但我只打印元音。但是,有些测试用例没有通过。
I am not removing anything but I am printing only vowels. But, some test cases didn't pass. Maybe this code doesn't work on multiple test cases.
推荐答案
尝试以下 用于 中的适当控制台:
Try this for proper console in :
int main()
{
int n;
std::cin >> n;
std::cin.ignore(); // fix
/* remaining code */
return 0;
}
>查找元音在字符串中
在字符串中查找元音的方式是使用 std :: binary_search $元音表中给定字符串的每个字符。
> To find the vowels in a string
On way of finding the vowels in a string is using a std::binary_search
each character of the given string in a vowel table.
- 对
char
进行排序的数组所有元音(即元音数组)。 - 对于每个
char
输入字符串std :: binary_search
在
元音数组中。 - 如果
std :: binary_search
返回true
(表示char
是元音),打印char
的字符串。
- Make a sorted array of
char
s of all vowels(i.e. vowels array). - For each
char
of the input string,std::binary_search
in the vowels array. - If
std::binary_search
returnstrue
(meaning thechar
is an vowel), print thechar
of the string.
以下是示例代码! (在线观看)
Following is the example code! (See live online)
#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 † ) 。
> To remove the vowels from a string
Use erase-remove idiom as follows(till c++17†).
- 对
char
进行排序的数组所有元音(即元音数组)。 - 使用
std :: remove_if
,收集指向字符(即元音)的迭代器。 lambda函数可用作std :: remove_if
的谓词,其中std :: binary_search
用于检查char
- 使用
std :: string :: erase
,从字符串中清除所有收集的字符(即元音)。
- Make a sorted array of
char
s of all vowels(i.e. vowels array). - Using
std::remove_if
, collect the iterators pointing to the characters, which are vowels. A lambda function can be used as the predicate forstd::remove_if
, where thestd::binary_search
is used to check thechar
in the string exists in the vowels array. - Using
std::string::erase
, erase all the collected characters(i.e. vowels) from the string.
以下是示例代码! (在线观看)
Following is an example code! (See live online)
#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 )
† Since c++20, one can use std::erase_if
for this, which would be less error prone than the the above one. (See online live using 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;
}
>删除辅音从字符串
要从给定字符串中删除辅音,请在上述谓词
中取反 std :: binary_search
。 (在线观看)
> To remove the consonants from a string
To remove the consonants from the given string, in the above predicate
negate the result of std::binary_search
. (See live online)
const auto predicate = [&a](const char str_char) -> bool {
return !std::binary_search(a.cbegin(), a.cend(), str_char);
// ^^ --> negate the return
};
作为旁注,
As side notes,
-
避免使用
#include< bits / stdc ++。h>
了解更多:为什么我不应该#include< bits / stdc ++。h> ;?
请勿使用命名空间std和练习;
了解更多信息:为什么使用命名空间std;被认为是不好的做法?
Do not practice with using namespace std;
Read more: Why is "using namespace std;" considered bad practice?
这篇关于如何从字符串中删除辅音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!