Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 26, 2005, 12:48 PM   #1
Sirena400
macrumors newbie
 
Join Date: Feb 2005
c++ question

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!

Last edited by Sirena400; Apr 26, 2005 at 01:49 PM.
Sirena400 is offline   0 Reply With Quote
Old Apr 26, 2005, 01:54 PM   #2
DXoverDY
macrumors 6502a
 
Join Date: Apr 2005
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
DXoverDY is offline   0 Reply With Quote
Old Apr 26, 2005, 03:03 PM   #3
NtotheIzoo
macrumors regular
 
Join Date: Jan 2005
Quote:
Originally Posted by Sirena400
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!
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[i]){
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..
NtotheIzoo is offline   0 Reply With Quote
Old Apr 28, 2005, 12:48 PM   #4
Sirena400
Thread Starter
macrumors newbie
 
Join Date: Feb 2005
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!
Sirena400 is offline   0 Reply With Quote
Old Apr 29, 2005, 12:11 PM   #5
NtotheIzoo
macrumors regular
 
Join Date: Jan 2005
No, problem...glad I could help!
NtotheIzoo is offline   0 Reply With Quote
Old May 1, 2005, 07:00 PM   #6
Beckie
macrumors newbie
 
Join Date: Apr 2005
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;
}
Beckie is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Mophie Juice Pack Plus (iPhone 5) Question - Mute Button Question JulesK iPhone Accessories 3 Mar 30, 2014 03:58 PM
Snow Leopard File Vault Question and Basic System Password Question & Time Machine? GordonGekko999 Mac Basics and Help 0 Oct 25, 2013 07:06 AM
Question for anyone who has seen or owns the pink ipod nano? Color deciding question. Pinkstiletto66 iPod 4 Apr 4, 2013 01:57 PM
Family Share Question, Upgrade Question, Argh head hurting.. Jazwire iPhone 1 Sep 14, 2012 01:19 AM
Time Capsule - Newbie Question Wifi Question Alper1234 Mac Peripherals 1 Jun 23, 2012 10:17 AM

Forum Jump

All times are GMT -5. The time now is 05:26 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC