C++ Strings
I don't yet have a clear understanding of how to use the string class yet.
i'm stuck as how to get started. I have this program that wants you to use find, length and substr without any type of loop or switch to convewrt a string into it's pig latin equivalent. Assume the vowels are e i o u and you are to check for the irst occurence of a vowel if the first letter has vowel add way to the end otherwise continue checking for the first occurence of a vowel and if found move the letters to the end and add ay to the end.
for example: apple = appleway
strong = ongstray
Any help would greatly be appreciated
i'm stuck as how to get started. I have this program that wants you to use find, length and substr without any type of loop or switch to convewrt a string into it's pig latin equivalent. Assume the vowels are e i o u and you are to check for the irst occurence of a vowel if the first letter has vowel add way to the end otherwise continue checking for the first occurence of a vowel and if found move the letters to the end and add ay to the end.
for example: apple = appleway
strong = ongstray
Any help would greatly be appreciated
0
Comments
(http://www.cplusplus.com - great place to learn standard functions)
#include <stdio.h> //IO functions #include <string.h> // String library void main () { char theString[100]; // original string char pigLatin[100]; // piglatin output char temp[100]; // temp string char * chopper; // used to chop string up printf("Enter string: "); gets(theString); // get a string from the keyboard char vowels[]= "aeiouAEIOU"; // define vowels int checkString; // integer to hold position of first vowel checkString = strcspn (theString,vowels); // find first vowel if (checkString==0) // if first vowel is first letter { strcpy(pigLatin,theString); // copy orginal string strcat(pigLatin,"way\0"); // append "way" } else // otherwise { strncpy(temp,theString,checkString); // copy letters before first vowel to temp string chopper=strtok(theString,temp); // chop off those letters strcpy(pigLatin,chopper); // copy the remaining string to pig latin strcat(pigLatin,temp); // append copied letters strcat(pigLatin,"ay\0"); // append "ay" } printf("Pig Latin is: %s\n",pigLatin); // print and done. }edit:// this only works on single words btw, in order to make it work with sentences, use the same chopper code in a simple loop, and chop on " " (check the link for how to do this) and then pass each delimited string to the vowel check seperately.
m
If it finds the letter you are looking for with the .find function it will return the index of the letter. Now you have to use the .substr from 0 to that index value returned to take off the front of the string and you can store that in a temporary string.
Use the index for the vowel and substring again to get the final part of the word and store that in another temporary string.
Finally, you can use + or .strcat to add the strings together and tack on the "way".
for example say you have this:
int main()
{
string word;
int pos;
int position;
cout << " Enter A Word: ";
cin >> word;
pos = word.find("a");
if (pos != string::npos)
position = pos;
pos = word.find("e")
if(pos != string::npos)
if(pos < position)
position = pos;
}
this is the problem, it bombs once this is working i can use substr to chop the preciding letters off that are in front of the vowel i find.
cplusplus.com and the MSDN website can help you out.