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

  • Thanks..
    1 week 3 days ago
  • Hmmm,Interesting one,thx for
    2 weeks 4 days ago
  • Buyer beware
    3 weeks 2 days ago
  • REPLY:Actual Processes Involved
    3 weeks 4 days ago
  • Back to Ruby
    12 weeks 5 days ago
  • Links provided
    28 weeks 1 day ago
  • Module for wordpress to Drupal 6.x
    28 weeks 1 day ago
  • The wordpress plugin looks
    29 weeks 6 days ago
  • Good point..... You're
    29 weeks 6 days ago
  • Many thanks. If I'm going to
    29 weeks 6 days ago

New forum topics

  • Imported posts only visible to user1
  • 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
more

Sponsored links

Steve's Stuff
All about my running and from time to time other stuff

Improve Memory
All about memory techniques, excellent and relevant articles.

Remove Vowels

  • View
  • Revisions
Submitted by Steve on Sat, 29 Dec, 2007 - 10:34
  • C++
  • short program

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
‹ Calculate Pace (Running Program) - # 2 up Remove Vowels - # 2 - String version ›
  • Printer-friendly version
  • Add new comment
Wed, 16 Jan, 2008 - 21:41
#1
Steve
User offline. Last seen 5 hours 30 min ago. Offline
Joined: 27 Mar 2008
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
Top
  • reply
  • Post reply

 Subscribe in a reader

free hit counter


RoopleTheme