Remove Vowels - # 2 - String version
I've modified the Remove Vowels program to now account for string use instead of straight out C-style string. The purpose of the program hasn't changed and that is to accept user input of a few words and strip out all the vowels. Real earth shattering stuff.
Additional to the previous version is that both upper and lowercase characters are now accounted for.
In order to reduce clutter at the end of the main() function, I reduced the exiting procedure to a function 'keep_window_open()'. On some Windows setup, it's a real pain that when running the program from the IDE the command prompt window closes automatically at the conclusion of the program. This function addresses that. If it's not needed simply comment it out.
Here's the code:
// Steven Taylor
// 16 Jan, 2008.
// 3 Mar, 2008 : demonstrate using type string
#include <iostream>
#include <string>
#include <cctype> // tolower() function
using namespace std;
const string VOWEL_IS = "aeiou";
// for caps - see use of tolower() function below.
string strip(const string &str);
bool isvowel(const char c);
void keep_window_open();
int main()
{
string str;
cout << "Enter a string, any old string of words:\n";
getline(cin, str);
cout << "\n\nThe user input without vowels :\n" << strip(str) << endl;
keep_window_open(); // less clutter.
return 0;
}
As you can see the main() function is clutter free and very simple. It does rely upon the following functions.
{
int index = 0;
int length = str.size();
char temp[length];
for (int i = 0; i < length; i++)
{
if (!(isvowel(tolower(str[i]))))
{
temp[index] = str[i];
index++;
}
}
temp[index] = '\0';
return (string)temp; // return temp; also works.
}
bool isvowel(const char c)
{
for (int j=0; j < 5; j++)
{
if (c == VOWEL_IS[j])
return true;
}
return false;
}
void keep_window_open()
{
// only needed if the command prompt window doesn't
// stay open. Using a function - assists
// with readability of the main() program
// that is, less clutter.
cout << "\n\n...Press ENTER to Exit System...";
cin.clear();
while (cin.get() != '\n')
continue;
cin.get();
}
I'm open to suggestions, good, bad or indifferent. Are there any other ways to achieve the purpose of this program? I'm sure there are. Whether they are more efficient or not is not the point. The point is that it gets the mind ticking considering options and alternatives. I know for one thing, I haven't explored all the std::string functions.
Consider this. If you're next assignment is along the lines of, 'check a user inputted string and remove all the characters from that string that are a part of a secondary user inputted string', that should be a piece of cake. In a few days or so, might submit such an example, based predominately upon the 'remove vowels' example.
| Attachment | Size |
|---|---|
| RemoveVowels-4.cpp | 1.53 KB |
- Printer-friendly version
- Login or register to post comments
- 519 reads
In keeping with the theme of this particular example, that is, using string based components, I realised late last night that the 'strip()' function wasn't playing the game. That is, the returned string was built using character array methods, not that there's anything wrong with that (Seinfeld).
So here is the modified string version of the strip() function. No other changes required to other parts of the program.
{
int length = str.size();
string result;
for (int i = 0; i < length; i++)
{
if (!(isvowel(tolower(str[i]))))
result = result + str[i];
}
return result;
}
You will note that this is cleaner looking (my technical term) and easier to follow. It's actually 5 lines of code less that the character array version. It could be reduced a further line of code by not using the 'length' variable and substitute 'length' in the for loop with
str.size()directly.