简体   繁体   中英

Remove vowels from string C++

So I am new to C++ and tried to create a function that would remove vowels from a string, but I am failing at it horribly, Here is my code so far :

#include <string>
using namespace std;
string remove(string st) {
    for(int i = 0; st[i] != '\0'; i++) 
        st[i] = st[i] == 'a' || st[i] == 'e' || st[i] == 'i' || st[i] == 'o' || st[i] ==
        'u' || st[i] == 'A' || st[i] == 'E' || st[i] == 'I' || st[i] == 'O' || st[i] ==
        'U' ? '' : st[i];
    }
return st;

This seems to throw an error ? any idea what I am doing wrong

The error I get is :

main.cpp:10:16: error: expected expression 'U' ? '' : Z[i];

And running on another interpreter :

   .code.tio.cpp:7:14: error: incompatible operand types ('const char *' and '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' (aka 'char'))
            'U' ? "" : Z[i];
                ^ ~~   ~~~~

The canonical way to remove elements from a sequential containers according to a predicate (a condition) is to use std::remove_if . Unlike it's name implies, this standard algorithm doesn't quite remove the elements, it moves them to the back of the container so they are easy to erase, leaving the other elements intact and in the same order. It returns an iterator that indicates the beginning of the portion of the container which contains the "removed" elements. Since the standard algorithms cannot change the size the containers they operate on, these elements must then be removed using the container's appropriate remove method. In the case of std::string , that's std::string::erase .

std::remove_if accepts a pair of iterators which define the range of elements to inspect, as well as a predicate that is used to determine which elements to remove. Elements for which the predicate is true are removed.

#include <algorithm>
#include <iostream>
#include <string>

// Returns true if p_char is a vowel
bool is_vowel(const char p_char)
{

    constexpr char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
    return std::find(std::begin(vowels), std::end(vowels), p_char) != std::end(vowels);
}

std::string remove_vowel(std::string st) 
{
    // Moves all the characters for which `is_vowel` is true to the back
    //  and returns an iterator to the first such character
    auto to_erase = std::remove_if(st.begin(), st.end(), is_vowel);

    // Actually remove the unwanted characters from the string
    st.erase(to_erase, st.end());
    return st;
}

int main()
{
    std::cout << remove_vowel("Hello, World!");
}

'' is not a valid character, you cannot place an "empty" one (such thing does not exist in C++) into the string to remove content...

What you can do is moving the non-vowels, ie the consonants, to front, skipping the vowels, and afterwards erase the surplus characters at the end:

auto pos = st.begin();
for(auto c : st)
{
    if(isConsonant(c))
        *pos++ = c;
}
st.erase(pos, st.end());

Edit: As François (correctly) denotes: There is no need to re-invent the wheel (provided you are not disallowed to use the standard library):

st.erase(std::remove_if(st.begin(), st.end(), [](char c) { return isConsonant(c); }), st.end());

Be aware that std::remove_if (as well as std::remove ) "remove" by just moving the elements to remain to front and returns an iterator to the new end of data – but does not really remove the elements "behind" the new end. So it is necessary to explicitly erase them as shown above.

So I am new to C++ and tried to create a function that would remove vowels from a string, but I am failing at it horribly, Here is my code so far :

#include <string>
using namespace std;
string remove(string st) {
    for(int i = 0; st[i] != '\0'; i++) 
        st[i] = st[i] == 'a' || st[i] == 'e' || st[i] == 'i' || st[i] == 'o' || st[i] ==
        'u' || st[i] == 'A' || st[i] == 'E' || st[i] == 'I' || st[i] == 'O' || st[i] ==
        'U' ? '' : st[i];
    }
return st;

This seems to throw an error ? any idea what I am doing wrong

The error I get is :

main.cpp:10:16: error: expected expression 'U' ? '' : Z[i];

And running on another interpreter :

   .code.tio.cpp:7:14: error: incompatible operand types ('const char *' and '__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type' (aka 'char'))
            'U' ? "" : Z[i];
                ^ ~~   ~~~~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM