0

Im trying to create a function to remove vowels from a string with spaces (not a cstring).
I need to pass the string into the function and have it add every consonant to another string that will return the string without the vowels. I need to keep the original string that has both the vowels and the consonants. Im stuck on how to check and add a consonant to the returning string. ex: input: Alexander Hamilton output: lxndr Hmltn

#include <iostream>
 #include <string>
using namespace std;

string removeVowels(string input, int size);

int main()
{
    string input;
    string vowlessFinal;
    getline(cin, input);
    cout << input;
    vowlessFinal = removeVowels(input, 20);
    cout << vowlessFinal;

}

string removeVowels(string input, int size) {
    string vowlessFinal;
    int j = 0;
    for (int i = 0; i < input.size(); i++) {
     //i have just 'a' below but i need to have the rest (including caps)   
    if (input.at(i) != 'a') {
            vowlessGuess.at(j) = input.at(i);
            j++;
        }
    }
    return vowlessFinal;
}
Zandalf
  • 11
  • 1
  • You'll need to either change the input, or return an output. In order to change the input, you need a reference. Basic function stuff. – Kenny Ostrom Mar 18 '20 at 02:22
  • terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 1) >= this->size() (which is 1) Without vowels thats: Aborted – Zandalf Mar 18 '20 at 02:23
  • If it runs, then it successfully compiled, but you had bugs. This is an intro debugging error, though. Index out of range means the index is out of range. Learn how to use the std::string class, particularly the size() function which tells you how long the string is. – Kenny Ostrom Mar 18 '20 at 02:25
  • 1
    That is NOT a compiler error. You said it won't compile. The error you wrote here is because `vowlessGuess` does not have sufficient space allocated to it. Use `string::push_back` instead. Or it could be that `size` is larger than `input.size()`. – paddy Mar 18 '20 at 02:25
  • 1
    Here is an approach using modern language features: https://godbolt.org/z/stsqFy – paddy Mar 18 '20 at 02:41
  • Does this need to support Unicode? Ä is a vowel too. – tenfour Mar 18 '20 at 08:49

2 Answers2

0

This should cover all cases. Pass-by-reference for original string, and for string-to-be-altered, which should be an empty string declared by caller:

void removeVowels(const std::string& input, std::string& result) {
    bool test = false;
    const std::string targets = "aAeEiIoOuU";
    for (const char& letter: input) {
        for (const char& target: targets) {
            if (target == letter) {               #test each vowel against string
                test = true;                      
                break;                            #exit loop if found
            }
        }
        if (!test) {
            result.push_back(letter);             
        }
        test = false;
    }
    return result;
}

The strategy is 'if test letter matches on any vowel, set flag and exit loop; if not flag, add that letter to new string; reset flag for next vowel loop.'

neutrino_logic
  • 1,289
  • 1
  • 6
  • 11
0

Hm. I am wondering if people do not know, or if there are other reasons, but C++ has a dedicated function for replacing something by something else: std::regex_replace

With that you can replace any pattern by something and assign the result to a new string. Then you can work with a one liner:

Please see the following example:

#include <iostream>
#include <string>
#include <regex>

int main() {

    // The test string
    std::string input{"The quick brown fox jumps over the lazy dog."};

    // Use simply the replace function
    std::string output = std::regex_replace(input, std::regex("[aeiouAEIOU]"), "");

    // SHow the output
    std::cout << output << "\n";

    return 0;
}

Please see also this answer. You can calculate, if a letter is a vowel.

A M
  • 14,694
  • 5
  • 19
  • 44