hi! can anyone solve this string-array problem or give me just an example about it

  • Write a program that converts a sentence input by the user into pig latin. You can assume
    that the sentence contains no punctuation. The rules for pig latin are as follows:

a) For words that begin with consonants, move the leading consonant to the end of the word
and add “ay”. Thus, “ball” becomes “allbay”; “button” becomes “uttonbay”; and so
forth.
b) For words that begin with vowels, add “way” to the end. Thus “all” becomes “allway”,
“one” becomes “oneway”; and so forth

I'll do it like this:
Have a function that returns how many words are in a line, i.e. counts the number of spaces.
Have a boolean function that checks if a string starts with a vowel or not
Tokenize the entire line.
Split the line into words.
Check each word if starts with a vowel or not.
Do the proper modification to the strings acordingly:

int spaces(string line)
    spaces<-number of words from a
end_spaces

boolean isVowel(string word)
    if a starts_with('a' || 'e' || 'i' || 'o' || 'u') then
        isVowel<-true
    else
        isVowel<-false
    end_if
end_boolean

subalg main
    line<-input
    tokenize(line)
    for i=0 to spaces(line) do
        part<-split(line, ' ')
        if isVowel(part) then
            part.append("way")
        else
            part.append(part[0])
            erase part[0]
            part.append("ay")
        end_if
        @store part in a container for a later access to the elements.
    end_for
end_main

Note that this is written in pseudo-code.
Links:
Tokenizing: Click Here
String: Click Here
Get entire line from input/Splitting: Click Here
Container: Click Here

Edited 4 Years Ago by Lucaci Andrew

thanks for your shared time, but I'm a beginner in C++ your written answer is a bit hard to understand. would you please make it simple for me?

Edited 4 Years Ago by akashi

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//declaration
string originalWord = "";       
void pigLatinString();
bool isPuncutation(char);

void main()
{
    cout<<endl;    //Welcome logo
cout <<"        .....:~::~~=~~~==+=+....................... ...:~~~~~~~~==~....." << endl;        
cout <<"        ....~~~~~~====~===~::::::::~~~:~~~~:::~:.....~~~~~~~~~::~===...." << endl;        
cout <<"        ..,~~~~=========~:::::~~~~~~~~~~~~~~::~~~~~::~~~~~:~~~~~~~=+=..." << endl;       
cout <<"        ..~~====++++==:::~:~~~::~~:~~:~~~:~~~~~~~~~~~::::~~:~======+=~." << endl;         
cout <<"        ,~===++??=~::~:::~::~~:~~~~:~~::~~~~~~~~~~~~~~~~~~=======+=+++=:" << endl;        
cout <<"    ...:~~==++??=:::::~~:::::::~:~~~~~~~~~~~~~~~~~~~~~~~~~:~=++=+=++++==,..." << endl;   
cout <<"    ..~~==++???~~~~~:~~::::::::::~~:~~::~~:~~:~~~~~~~~~~~=~==++++++++++==,.." << endl;    
cout <<"    :~~===?+?+~~~:~~::~:::::::::~~~~~~~~~~~~~~:::~~~~~~=~=====+?+++=+??+==,." << endl;    
cout <<"...=+===++I?+==~~~~~::::::::::::~:~~~~~~~~~~~~~::::~~~~=~=====++++??++???==+...." << endl;
cout <<"..++=++??III==~~~~~~=::::::::::~~~~~~~~~~=~~~~~~~:~~~~~~======+++++??++++++==. ." << endl;
cout <<"..,?I?III77?==~~~~::::::::NNN,:~~~~~~~~~~~~~~~==~~~~~~~~===+==++++??????+?+++=,." << endl;
cout <<".....I7III7$+==~~~~~~~:$,,~~::+~~~~~~~~==~~~~======~===~====+?=+++?IIII????I??I," << endl;
cout <<"...........,==~~~~~~~~~~~~~:~:~:~~~~~~=~====~=~===:$MM7,++===++++??I7II7III7I?,." << endl;
cout <<"        ....=======~~~~~~~~~~~~~~~~~==~==========O,~=~=,M=++++++++??77777777?..." << endl;
cout <<"        ...=~====~=~~===+=+===~======================+====++?+++?+???..........." << endl;
cout <<"        ...========~~=+=D$ON~===~=========+=+=++=++++++++++++??+++?+=..........." << endl;
cout <<"        ..============$N,,,::?~===============++?IIO7++++?++??++??+++,..        " << endl;
cout <<"        ..=====~==~==+M.,,,,:M================++M8,::ON+++++?++??????+..        " << endl;
cout <<"        .=====~+==~===$::,,,:M~==+=======+=+==I=N:::~:N7+++++?++++??++..        " << endl;
cout <<"    ....~+===========~?,IMM.:Z:============++++M,~:~=~,M++++??I??????+.         " << endl;
cout <<"    ..,=++===========~7NNNDMMM:+=======+++++=?=N:,..,=.N+?++??????????:.        " << endl;
cout <<"......+==============+MNN..NNM=~==+===++++=+++~D+MNMNM,M++????????????+.....    " << endl;
cout <<" ....+==~==========~==,NNNMNN:~~========++==++~MNN$,NNM?++??+?????????+?....    " << endl;
cout <<"...,====~~~~====~==~~~:~~NN~=:~~=~==========++?+M:MMMNM~?+?+??+:?+???+++~...  . " << endl;
cout <<"..:+==~~~~~~~~=~:~~~~:~:~::~::::~~~~~~==~=~==~~~D~~NMM~+~+~~??+~????~++~==..    " << endl;
cout <<"..===~~~~~~~~:~~~~~~~~:~:::~~~~::::~::~:~~::::~~::~~~=~=++?+???+????++====+.    " << endl;
cout <<".++====~~~:~:~~~~~~~:::::::::~~~~~~~~~~~~~~~:~::::~~~~=~+++?+??++?+=+=+=====    " << endl;
cout <<"+++=====~~~~~~~~~~~~::::::~:~:~~~~~~~~~~~~~=~~~~~~~~~~=~===++++++=======+=+==..." << endl;
cout <<"?++++===~~~~===~~~~~::::~~~~~:~~====~=~~~~~~~===~~~~~=======+++==========+=++?.." << endl;
cout <<"I?+++===?====+==~~~:~:~~:~::~~============~~~~==+========+=++++==========+++++.." << endl;
cout <<"I??++++++++++=====~~~~~~~:~~=========+===========+======+=+++?+=========+++?+?.." << endl; 
cout <<"I?I??+++++??++====~~~~~~~===================+====++++==+=++++++++====+++?++??I.." << endl;
cout <<"I?I???????II?++===~=~~=~=====++++======++++=+=++++++++++++++?????+++?=??I????I.." << endl;
cout <<".II?????+++++=======+=~=======$7=+=++++88=+++==+=+?++++++++????I?++++I+????III.." << endl;
cout <<"..I????++++==~=++===+=========$7?+==++I$$~++=+===+=+??+?+????IIII??????+???I7:.." << endl;
cout <<"..?7?I??+?+===~=+============~7I+++==+OO7~++=++++++????+?+?IIIIII???????I??II..." << endl;
cout <<"...,7???++++++=~=++===========ZZ===+++=$?~+++=++++????????II?=?I7???I??????I~..." << endl;
cout <<".....?I???++++====+++======+=++==+=+++=~===++++++???????III?++?IIII7IIIIIIII...." << endl;
cout <<"     .I?????++++==~~?++=+++=+=+=++++=+++++=+++++??????II7I??????IIIIIIIIIII.    " << endl;
cout <<"    ...?II+++++++==~=+???+++++++++++++++++?++????IIII$7????I???IIIII7I7III..    " << endl;
cout <<"    ....?I???++++++==++?I?+?I???+++?+?++???????IIII8$?????????III7I7II7II...    " << endl;
cout <<"        ..II??++=+++=+++?III7$I???II?IIIII?I+I$Z$$$7??I??II7?III77II777,....    " << endl;
cout <<"        ...+7????++++==++?IIII77$$$ZZ$$$Z$$Z$$ZZ$7I????II?IIIIIIIIII7......     " << endl;
cout <<"            .II+??+++===+??I????77$7$ZOZZZZ$ZZ$$$I????IIIIIII7I?7$?.            " << endl;
cout <<"            ...I??+++=====++I=+=?I7777$$ZZ$$$$$7II???IIIIII7777$:...            " << endl;
cout <<"            .....I???+++===++?+=+????I777$II7?????I?IIII7I77II......            " << endl;
cout <<"                 ..II??++=++=+++++?????I$+?????I??III77I7$$~....                " << endl;
cout <<"                 ....=II??+===+++====+===+=++???IIII7777,.......                " << endl;

    cout<<"\n\t"<<setw(23)<<"@####"<<setw(1)<<"###";
cout<<"\n\t"<<setw(21)<<"@###"<<setw(7)<<"###\n";
cout<<"\t"<<setw(20)<<"###" <<setw(8)<<"##\n";
cout<<"\t"<<setw(22)<<"###\n";
cout<<"\t"<<setw(23)<<"###\n";
cout<<"\t"<<setw(25)<<"###\n";
cout<<"\t"<<setw(26)<<"###\n";
cout<<"\t"<<setw(17)<<"##"<<setw(9)<<"###\n";
cout<<"\t"<<setw(18)<<"###"<<setw(8)<<"###@\n";
cout<<"\t"<<setw(23)<<"#######"<<setw(2)<<"@___________________'S\n";

    cout<<"\t\t*****************************************************"<<endl;
    cout<<"\t\t*     English Language to Pig-Latin Converter       *"<<endl;
    cout<<"\t\t*  --------------------^^^^^------------------------*"<<endl;
    cout<<"\t\t*  Please Type your Expected Words and Enjoy  the   *"<<endl;
    cout<<"\t\t*                    Different                      *"<<endl;
    cout<<"\t\t*****************************************************"<<endl;

    cout<<endl;

    while(1)
    {    
        cout<<"\t\t____________________________________________"<<endl;
        cout<<"\n\t\tEnter a group of words or ENTER to quit: "; //Prompt the user to enter a line of words

        //input
        getline(cin, originalWord);
        cout<<"\t\t____________________________________________\n"<<endl; //taking a line as an input

        if(originalWord == "") //If the user has pressed enter
        {
            break;  
        }
        cout<<"\n\t\t\t\t  You Typed : " + originalWord<<" "; //displaying the original word
        pigLatinString(); //Function that is used to convert a sentence to pig latin
     }

    system("pause"); 
}

void pigLatinString()//Function that is used to convert a sentence to pig latin
{
    //initializing variables
    int len, counter, start, begin;
    string  newString;
    string word[1000];
    start = 0; 
    begin = 0;  
    newString = "";
    counter = 0;
    while(1)
    {
        //process
        start = originalWord.find(' ', start); //function that is finding the occurance of space privided the position from where to shart checking
        if(start == string::npos) //If no more spaces found and string ends
        {
            break; //Break the loop and come out
        }
        len = start - begin; //Calculating word length
        word[counter] = originalWord.substr(begin, len); //getting the word

        int index = -1;
        for(int i = 0 ; i < word[counter].length(); i++)
        {  //Checking for vowel character
            if(word[counter][i] == 'a'|| word[counter][i] == 'e' || word[counter][i] == 'i' || word[counter][i] == 'o' || word[counter][i] == 'u' || word[counter][i] == 'y'
             || word[counter][i] == 'A'|| word[counter][i] == 'E'|| word[counter][i] == 'I'|| word[counter][i] == 'O'|| word[counter][i] == 'U' || word[counter][i] == 'Y')
            {
                index = i;
                break;
            }
        }
        if(index == 0)
            newString = newString + word[counter]  +  "way ";  // Adding "way" with vowel
        else if (index == -1)
            newString = newString + word[counter] + "ay ";   // Addinh "ay" with consonent
        else
            //updating newString variable and saving PigLatin word
            newString = newString + word[counter].substr(index, word[counter].length() - index) + word[counter].substr(0, index) +  "ay "; 

        start = start + 1; //Incrementing start inex so that it points to next character nd start reading from there
        begin = start; //Updating begin variable
        counter = counter + 1; //Updatng word coutner
    }

    len = originalWord.length()- begin; //Calculating last word's length
    word[counter] = originalWord.substr(begin, len); //Getting last word

    int index = -1;
        for(int i = 0 ; i < word[counter].length(); i++)
        {   //Checking for vowel character
            if(word[counter][i] == 'a'|| word[counter][i] == 'e' || word[counter][i] == 'i' || word[counter][i] == 'o' || word[counter][i] == 'u'
             || word[counter][i] == 'A'|| word[counter][i] == 'E'|| word[counter][i] == 'I'|| word[counter][i] == 'O'|| word[counter][i] == 'U')
            {
                index = i;
                break;
            }
        }
        if(index == 0)
            newString = newString + word[counter]  +  "way ";  // Adding "way" with vowel
        else if (index == -1)
            newString = newString + word[counter] + "ay ";      // Addinh "ay" with consonent
        else
            //updating newString variable and saving PigLatin word
            newString = newString + word[counter].substr(index, word[counter].length() - index) + word[counter].substr(0, index) +  "ay "; 


         counter = counter + 1; //incrementing word counter

     for(int i = 0; i < newString.length(); i++) // converting string to lower characters
        newString[i] = tolower(newString[i]);
     //output
    cout<<"\n\t\t\t     Pig_Latin Word : "<<newString<<endl<<endl; //display new string after pig latin conversion
}

Inline Code Example Here

Here's my version, it's quite old, from your first post...

/*
 * vulgar_latin.cpp
 * vulgar_latin.cpp is licensed under GNU GENERAL PUBLIC LICENSE
 *  Created on: Oct 19, 2012
 *      Author: sin
 */
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
using namespace std;

int spaces(string s){
    int j=0;
    if (s.size()!=0) j++;
    for (size_t i=0;i<s.size();i++){
        if (s[i]==' ') j++;
    }
    return (j);
}

bool isVowel(string s){
    if (s[0]=='a' || s[0]=='e' || s[0]=='i' || s[0]=='o' || s[0]=='u' ||
        s[0]=='A' || s[0]=='E' || s[0]=='I' || s[0]=='O' || s[0]=='U'){
        return (true);
    }
    return (false);
}


int main(){
    vector<string> list;
    cout<<"Insert sentence: ";
    string line;
    getline(cin, line);
    stringstream token(line);
    for (int i=0;i<spaces(line);i++){
        string part;
        bool punct=false;
        char punctu;
        getline(token, part, ' ');
        if (ispunct(part[part.size()-1])){
            punct=true;
            punctu=part[part.size()-1];
            part.erase(part.end()-1);
        }
        if (isVowel(part)){
            part.append("way");
        }
        else{
            bool isup=false;
            if (isupper(part[0])){
                part[0]=(char)(((int)part[0])+32);
                isup=true;
            }
            part.insert(part.begin()+part.size(), part[0]);
            part.erase(part.begin());
            part.append("ay");
            if (isup){
                part[0]=(char)((int)part[0]-32);
            }
        }
        if (punct){
            part.insert(part.end(), punctu);
        }
        list.push_back(part);
    }
    for (size_t i=0;i<list.size();i++){
        cout<<list[i]<<" ";
    }
    return (0);
}

It also handles the punctuation characters, and upper/lower case ones, in general, not taken to extreme...
Note that is done in "hurry", and it's kinda raw, but you got the point.

/*
 * Input:
 *Insert sentence: Hello world, nice to meet you all.
 * Output:
 *Ellohay orldway, icenay otay eetmay ouyay allway. 
 */

Edited 4 Years Ago by Lucaci Andrew

This question has already been answered. Start a new discussion instead.