Remove Vowels

Perusing the forums I came across this thread a question relating to inputting a string and then redisplaying the string without the vowels. What I failed to notice was that the thread was a C thread (not C++) and so my C++ solution, not precisely addressing the original posters question. This, not such a problem for me, as it helped cement my learning to date.

This took me about 15 or 20 minutes to nut out the basic solution and then another 40 or 50 minutes to re-visit passing and returning of arrays from functions. In essence, this little routine re-enforced the fact that arrays are passed by reference and in effect are pointers. Therefore returning an array is merely a matter of modifying an argument parameter.

A particular enhancement would be to also account for capital vowels, which I see would be easy to incorporate. The main thrust of the problem was how to strip characters from a string and display them to the user. This, I've achieved.

There's room for enhancement to this solution and I'm open to any suggestions or recommendations. I know in time I'll probably revisit this and post a comment with updated or improved coding technique.

Remember, I've written this whilst into my 6th week of self-study (haven't touched on classes yet).

// RemoveVowels-2.cpp  --  Remove vowels from an inputted string
// http://www.gidforums.com/t-16756.html
// Steven Taylor
// 29 Dec, 2007.

#include <iostream>
using namespace std;
const char VOWEL_IS[] = "aeiou";  // consider including CAPS

void strip(const char *str, int length, char *newstring);
bool isvowel(const char c);

int main()
{
    char str[80];
    cout << "Enter a string, any old string of words:\n";
    cin.get(str,80);
    char withoutvowels[80];
    strip(str,80, withoutvowels);
    cout << "\n\nThe user input without vowels :\n" << withoutvowels << endl;

    // exit routine
    cout << "\n\n...Press ENTER to Exit System...";
    cin.clear();
    while (cin.get() != '\n')
        continue;
    cin.get();
    return 0;
}

And now the functions.

void strip(const char str[], int length, char *newstring)
{
    int index = 0;
    for (int i = 0; i < length; i++)
    {
        if (!(isvowel(str[i])))
        {
            newstring[index] = str[i];
            index++;
        }
    }
    newstring[index] = '\0';
}
bool isvowel(const char c)
{
    for (int j=0; j < 5; j++)
    {
        if (c == VOWEL_IS[j])
            return true;
    }
    return false;
}

So, that's it. The above is a file, for download, in the attachments.

AttachmentSize
RemoveVowels-2.zip724 bytes
Steve
Joined: 27 Mar 2008
User offline. Last seen 19 hours 4 min ago.
Remove vowels - version 2

I mentioned in the initial post

Therefore returning an array is merely a matter of modifying an argument parameter.

It's not the only way and I remember when initially coding it I was wanting the function to return the modified string, got confused and went with argument passing by reference. That seemed to be easier.

I now know how to return a string or more precisely a character string (array) to the calling function, in this case int main().

Here's the modified strip function only. Obviously to use this version the prototype and how it's used in the main program will have to be altered accordingly.

char *strip(const char *str, int length)
{
    int index = 0;
    char temp[length];
               
    for (int i = 0; i < length; i++)
    {
        if (!(isvowel(str[i])))
        {
            temp[index] = str[i];
            index++;
        }
    }
    temp[index] = '\0';
    char *final = new char[index];
    strcpy(final,temp);
    return final;
}

I'm open to any suggestions.

n/a

free hit counter