Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

string manipulation and condition

Posted on 2005-05-08
4
Medium Priority
?
277 Views
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) == vowels.at(k)) //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) == conso.at(w))  //true if  consonant begins      
                     for (int z=0;z<vowels.size();z++)
                       if (words[i].at(1) == vowels.at(z))
                         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";
                           
                           
                   }
                   
         }
         
}
0
Comment
Question by:kevinng
2 Comments
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 1000 total points
ID: 13985407
Kevin,

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

if ( starts with vowel )
{
 // Just add "yay"
}
else
{
 // 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.
}
else
{
 // 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
useful.

Paul
0
 
LVL 14

Expert Comment

by:wayside
ID: 15622424
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.

wayside
EE Cleanup Volunteer
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

578 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