如何从字符串中删除辅音? [英] How to remove consonants from a string?

查看:167
本文介绍了如何从字符串中删除辅音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

#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.


  1. char 进行排序的数组所有元音(即元音数组)。

  2. 对于每个 char 输入字符串 std :: binary_search
    元音数组中。

  3. 如果 std :: binary_search 返回 true (表示 char 是元音),打印 char 的字符串。

  1. Make a sorted array of char s of all vowels(i.e. vowels array).
  2. For each char of the input string, std::binary_search in the vowels array.
  3. If std::binary_search returns true(meaning the char is an vowel), print the char 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






>要从字符串中删除元音



使用 擦除-删除惯用语 ,如下(直到 ) 。


> To remove the vowels from a string

Use erase-remove idiom as follows(till c++17).


  1. char 进行排序的数组所有元音(即元音数组)。

  2. 使用 std :: remove_if ,收集指向字符(即元音)的迭代器。 lambda函数可用作 std :: remove_if 的谓词,其中 std :: binary_search 用于检查 char

  3. 使用 std :: string :: erase ,从字符串中清除所有收集的字符(即元音)。

  1. Make a sorted array of char s of all vowels(i.e. vowels array).
  2. Using std::remove_if, collect the iterators pointing to the characters, which are vowels. A lambda function can be used as the predicate for std::remove_if, where the std::binary_search is used to check the char in the string exists in the vowels array.
  3. 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

,因为,则可以使用 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,

请勿使用命名空间std和练习; 了解更多信息:为什么使用命名空间std;被认为是不好的做法?

Do not practice with using namespace std; Read more: Why is "using namespace std;" considered bad practice?

这篇关于如何从字符串中删除辅音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆