string manipulation and condition

Posted on 2005-05-08
Last Modified: 2010-04-01

alright, i have enough of this string conditioning, just cannot get it right. Here is the function .

and there rule is .

i can handle the first vowel begins fine but the hardest part probably detect multiple consonants.


 For words which begin with a single consonant take the consonant off the front of the word and add it to the end of the word. Then add ay after the consonant. Here are some examples:

cat = atcay
dog = ogday
simply = implysay
noise = oisnay
For words which began with double or multiple consonants take the group of consonants off the front of the word and add them to the end, adding ay at the very end of the word. Here are some examples:

scratch = atchscray
thick = ickthay
flight = ightflay
grime = imegray

For words that begin with a vowel, just add yay at the end. For example:

is = isyay
apple =appleyay
under = underyay
octopus = octopusyay

void PigLatin::translator()

       string vowels = "aouie";
       string conso ="bcdfghjklmnpqrstvwxyz";
        vector <string>words;
  for (int i =0; i < num_of_words ; i++)
       {    words.push_back(result[i]);
            int word_length = result[i].size();
           for(int k = 0; k < vowels.size();k++)
                 if ( words[i].at(0) == //check if word begins vowel or not
                       result [i] = words[i] + "yay";
             for (int w =0; w < conso.size();w++)
                 if (words[i].at(0) ==  //true if  consonant begins      
                     for (int z=0;z<vowels.size();z++)
                       if (words[i].at(1) ==
                         result [i]= words[i].substr(1,word_length) + words[i].substr(0,1) + "ay";
                        else //false if only 1 consonants
                            result [i]= words[i].substr(2,word_length) + words[i].substr(0,2) + "ay";
Question by:kevinng
    LVL 16

    Accepted Solution


    You may find it easier to respecify the algorithm before implementing it. How about:

    if ( starts with vowel )
     // Just add "yay"
     // Move all consonants at the start to the end.
     // Add "ay"

    Since you are adding "ay" in both cases, you can recode this to:

    if ( starts with vowel )
     // Just add "y" at end.
     // Move all consonants at the start to the end.

     // Add "ay" at end.

    Now you can see a slight adjustment that makes coding simpler but slightly less efficient:

    if ( starts with vowel )
     // Add "y" at end.

    // Move all consonants at the start to the end.
    // Add "ay" at end.

    This, surely is easier to implement. Remember to write a separate function to detect whether a character is a vowel or a consonant. You might find
    strchr ("aeiou", word[0]) != NULL

    LVL 14

    Expert Comment

    No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
    I will leave the following recommendation for this question in the Cleanup topic area:

    Accept: PaulCaswell

    Any objections should be posted here in the next 4 days. After that time, the question will be closed.

    EE Cleanup Volunteer

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now