如何从字符串中删除辅音?
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
我没有删除任何东西,但我只打印元音。但是,一些测试用例没有通过。也许此代码不适用于多个测试用例。
尝试此操作以获得正确的控制台:
int main()
{
int n;
std::cin >> n;
std::cin.ignore(); // fix
/* remaining code */
return 0;
}
> 查找字符串中的元音
在字符串中查找元音的方法是在元音表中使用给定字符串的每个字符std::binary_search
。
- 制作一个所有元音的
char
s 的排序数组(即元音数组(。 - 对于输入字符串的每个
char
,std::binary_search
元音数组。 - 如果
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†(。
- 制作一个所有元音的
char
s 的排序数组(即元音数组(。 - 使用
std::remove_if
,收集指向字符的迭代器,这些字符是元音。lambda函数可以用作std::remove_if
的谓词,其中std::binary_search
用于检查元音数组中是否存在字符串中的char
。 - 使用
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;
}
相关文章:
- 如何删除字符串中的前 2 个字符(如果它有"-")
- 删除字符串后C++检测到堆损坏
- 如何在 c++ 中删除字符串中的某些字符?
- 删除字符串数组
- 删除字符串中具有相同值的任何相邻字母"pair"
- 删除字符串向量中的字符重复
- 如何在Qt中删除字符串(qstring)中的子字符串
- 删除字符串指针在struct中的内存
- 删除 C 字符串中的大写字母
- 如何搜索和删除字符串中的字符
- 如何删除字符串中的所有空格并将数据存储在两者之间
- 删除字符串中不平衡的括号
- 删除字符串数组中的换行符
- 如何修复我的代码以删除字符串中的空格
- 如何删除字符串所有可能的子字符串中带有空格的子字符串?
- 如何创建删除 C 字符串中所有选定字符的函数
- 传递GTK回调时,请在C 中删除字符串向量
- 为什么删除字符串迭代器会产生const char参考
- 从<string>向量<int>中定义的索引处的向量中删除字符串
- 如何使用 c++ 剪切或删除字符串中的某些字符