c++ question

Discussion in 'Mac Programming' started by Sirena400, Apr 26, 2005.

  1. macrumors newbie

    Joined:
    Feb 7, 2005
    #1
    Hey all, I'm trying to write a program to convert regular words into pig latin. to do that there's three rules: if the word starts with a vowel add way to the end, ex. apple=appleway. if the word has a vowel but doesn't start with it then take the consenants it front of the first vowel and put the to the end of the word and add ay to the end. ex: balll=allbay, strong=ongstray. and if the word has no vowels just add ay to the end. ex. pfft=pfftay. i have the basis of the program done but i can't figure out how to get the computer to recognize what's a vowel and what's not. here's what i got so far, you type in the input btw:

    #include<iostream.h>
    #include "apstring.h"

    int main()
    {
    apstring word;
    int x=0;
    cin>>word;
    switch(word[x])
    {
    case'a':case'e':case'i':case'o':case'u':case'y':
    {
    x=1;
    break;
    }
    }

    if(x=1)
    word=word.substr(x,word.length()-x)+word.substr(0,x)+("ay");
    else
    word=word+"ay";

    cout<<word<<endl;

    return 0;
    }

    for every word it just takes the first letter off, adds it to the end and then adds ay. i know that it's because x=1 that makes it just take off the first letter and add ay, but i don't know what else i should do to try and get it to work and recognize vowels. any suggestions on what to do? thanks!
     
  2. macrumors 6502a

    Joined:
    Apr 19, 2005
    #2
    First off you should check the last character of the string

    if you have apple then word[4] = e

    so.. grab the length of the string, subtract 1, then check if that character is a, e, i, o, u, y.. do the appropriate action.

    if the last letter is not one of those.. you can do this with the switch statement by the way, or just use if thens and if the last character isn't a vowel, do the appropriate action on else. or default for switch.

    also note you will probably want to take into consideration that you may not have a string. so if string = "" then catch that, and also account for capital letters. easiest way to do this is just to convert the whole string to lowercase first, then do your conversion to pig latin
     
  3. macrumors regular

    Joined:
    Jan 24, 2005
    #3
    here is a template

    //use this to locate position of first vowel
    int vowelLocation = -1;

    //use a loop to go through word till vowel found
    for (int i = 0; i < sizeofword; i++){
    switch(word){
    case'a':case'e':case'i':case'o':case'u':case'y':
    vowelLocation = i
    i = sizeofword
    break;
    }
    }

    //check if vowel is first letter
    if (vowelLocation == 0){
    //vowel first letter, add way
    ....
    }

    //check if there is vowel in word
    else if (vowelLocation != -1){
    //there is a vowel at vowelLocation
    //take letters before vowelLocation and
    //add to end of word + ay
    ...
    }

    //no vowel
    else{
    //no vowel, just add ay at the end
    ...
    }

    i hope this helps...i didnt want to write the whole thing for you and I am currently at work...didnt have much time..
     
  4. thread starter macrumors newbie

    Joined:
    Feb 7, 2005
    #4
    Thanks NtotheIzoo! I got the program working beautifully now! Lol, trust me, it helped. And I didn't want you writing the whole thing anyways, I'm learning C++ bit by bit, I just need some help now and then. Thanks very much! :)
     
  5. macrumors regular

    Joined:
    Jan 24, 2005
  6. macrumors newbie

    Joined:
    Apr 6, 2005
    #6
    I have the whole program in my book:
    I will see if I can copy it here:

    #include <iostream>
    #include <string>

    using namespace std;

    bool isVowel(char ch);
    string rotate(string pStr);
    string pigLatinString(string pStr);

    int main()
    {
    string str;
    cout << "Enter a string: ";
    cin >> str;
    cout << endl;

    cout << "The pig Latin form of " << str << " is: " << pigLatinString(str) <<
    endl;

    return 0;
    }

    bool isVowel(char ch)
    {
    switch (ch)
    {
    case 'A': case 'E':
    case 'I': case 'O':
    case 'U': case 'Y':
    ase 'a': case 'e':
    case 'i': case 'o':
    case 'u': case 'y': return true;
    default: return false;
    }
    }

    string rotate(string pStr)
    {
    string::size_type len = pStr.length();

    string rStr;

    rStr = pStr.substr(1, len - 1) + pStr[0];

    return rStr;
    }

    string pigLatinString(string pStr)
    {
    string::size_type len;

    bool foundVowel;

    string::size_type counter;

    if (isVowel(pStr[0]))
    pStr = pStr + "-way";
    else
    {
    pStr = pStr + '-';
    pStr = rotate(pStr);
    len = pStr.length();
    foundVowel = false;

    for (counter = 1; counter < len - 1; counter++)
    if (isVowel(pStr[0]))
    {
    foundVowel = true;
    break;
    }
    else
    pStr = rotate(pStr);

    if (!foundVowel)
    pStr = pStr.substr(1, len) + "-way";
    else
    pStr = pStr + "ay";
    }

    return pStr;
    }
     

Share This Page