Prime 357

We'll learn something

Site Menu

  • Home
  • Recent Posts
  • Forum
    • Programming Languages
      • C++
    • Website Design & Content Management
      • Wordpress >> Drupal
  • Blogs
  • Topics
    • C++
    • Changing hosts - Dummies Guide
    • Wordpress >> Drupal
  • Download Centre
  • Contact us
Home C++ (The Book) Short Programs


Image - OpenID

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password

Navigation

  • Recent posts

Topics

  • C++ (The Book)
    • Basic cin operations
    • Short Programs
      • Create a triangle type pattern
      • Array Solution - Half and half, 10 per line
      • Calculate Pace (Running Program)
      • Calculate Pace (Running Program) - # 2
      • Remove Vowels
      • Remove Vowels - # 2 - String version
    • Compiler/Linker Error Messages
  • Changing Hosts - a Dummies Guide
  • Wordpress to Drupal

Recent comments

  • Links working
    21 hours 42 min ago
  • Thanks... I may be able to
    1 day 3 hours ago
  • 3306 by default
    1 day 13 hours ago
  • Is this the right place to
    1 day 14 hours ago
  • Figured
    3 weeks 6 days ago
  • I'm guessing at this stage
    3 weeks 6 days ago
  • WordPress MU?
    3 weeks 6 days ago
  • Thanks
    10 weeks 1 day ago
  • I'm running the conversion
    11 weeks 4 days ago
  • Can't reproduce
    11 weeks 6 days ago

New forum topics

  • What should the port number be
  • WordPress MU?
  • funny little bug in mac version
  • Error: Unable to Insert into Node_revisions table when converting from wordpress 2.6.0 to drupal 6.4
  • index.php?
more

Who's online

There are currently 0 users and 2 guests online.

Who's new

  • oODeathStormOo
  • leruffiant
  • Emtee
  • mnogodet
  • ZioMimmo

Remove Vowels - # 2 - String version

  • View
  • Revisions
Submitted by Steve on Mon, 3 Mar, 2008 - 16:03
  • C++
  • string

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:

// RemoveVowels-4.cpp  --  Remove vowels from an inputted string
// 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.

string strip(const string &str)
{
    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.

AttachmentSize
RemoveVowels-4.cpp1.53 KB
‹ Remove Vowels up Compiler/Linker Error Messages ›
  • Printer-friendly version
  • Login or register to post comments
  • 452 reads
Tue, 4 Mar, 2008 - 12:39
#1
Steve
Joined: 27 Mar 2008
User offline. Last seen 7 hours 18 min ago.
Modified strip() function - string

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.

string strip(const string &str)
{
    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.

n/a
Top
  • Login or register to post comments

 Subscribe in a reader

free hit counter


RoopleTheme