C++ Strings

edited October 2004 in Internet & Media
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

Comments

  • mondimondi Icrontian
    edited October 2004
    something like this:

    (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
  • edited October 2004
    thanks for the response, unfortunately in c++ i need to utilize the functions find, substr and lenght using the string class and suggestions how to use these to check each occurence of a vowel and then moving the charactersto the end of the string would greatly be appreciated
  • JBJB Carlsbad, CA
    edited October 2004
    ok, what you are going to do is use the .find function to check if that word has a vowel.

    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".
  • edited October 2004
    I understand what i need to do, the problem is trying to test each oocurence of the vowel.


    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.
  • mmonninmmonnin Centreville, VA
    edited October 2004
    Yeah what mondi used was a null-termined array which is kinda like a string.

    cplusplus.com and the MSDN website can help you out.
Sign In or Register to comment.