Function help - Character Arrays.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Function help - Character Arrays.

  1. #1
    Join Date
    Mar 2009
    Posts
    35

    Function help - Character Arrays.

    In pig-latin if the word starts with a constonant, then all the letters of that word up to a vowel are added to the end.

    nerds -> erds-nay
    shake -> ake-shay
    translate -> anslate-tray
    I made this code to do this. Could someone look over it as I really havnt had much practice with arrays and cstring.

    Code:
    void con_PLatin (char word[], const int length)
    {
      char temp[20];
      char tempword[25];
      char chartemp;
      int i = 1; 
      int j = 0;
      int a = 0;
      int length_tempword;
      
      chartemp = word[0];
      strcopy (temp, chartemp);
      
      do 
      {
        
    	if (strchr("bcdfghjklmnpqrstvwxyz", word[i])
    	{ 
          chartemp = word[i];
    	  strcat (temp, chartemp);
    	  i++;
        }
      }	while(strchr("bcdfghjklmnpqrstvwxyz", word[i]);
    
      do
      {
        tempword[j] = word[j];
    	j++;
      }while (j < length);
      
      length_tempword = strlen(tempword);
      
      do 
      { 
        word[a] = tempword [a];
    	a ++ 
      } while (a < length_tempword);
      
      strcat (word, "-");
      strcat (word, temp);
      strcat (word, "ay");
     
      return;
    }
    Does it look good? Any changes or recommendations to make it better (without using more complex C++) ?

  2. #2
    Join Date
    Sep 2003
    Posts
    213

    Re: Function help - Character Arrays.

    Hi

    I would prefer to use string instead of char array
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main( void )
    {
    	string vowels = "aeiou";
    	string englishWord = "translate";
    	string latinWord = "";
    
    	if ( englishWord.find(vowels) )
    	{		
    		latinWord = englishWord.substr( englishWord.find_first_of(vowels), englishWord.length() ) +
    					"-" +
    					englishWord.substr( 0, englishWord.find_first_of(vowels) ) +
    					"ay";
    	}
    	
    	cout << latinWord << endl;
    	return 0;
    }
    Just something simple to demonstrate the idea. Might not be the best method

  3. #3
    Join Date
    Dec 2005
    Location
    England
    Posts
    86

    Re: Function help - Character Arrays.

    You can make it a lot easier on yourself by using std::string from the standard library. Also, there's another rule you missed: a word starting with a vowel simply gets ‘-way’ appended. For example, ‘eagle’ becomes ‘eagleway’:
    Code:
    #include <cstring>
    #include <string>
    #include <iostream>
    
    inline bool is_vowel(char ch) {
      return strchr("aeiou", ch);
    }
    
    std::string con_PLatin(const std::string word) {
      std::string lead, rest;
    
      for (int i = 0; i < word.length(); ++i)
        if (is_vowel(word[i])) {
          lead = word.substr(0, i);
          rest = word.substr(i, word.length());
          break;
        }
    
      return rest + (lead.empty() ? "way" : lead + "ay");
    }
    If you want to be able to pass C-strings, just overload to convert:
    Code:
    const char* con_PLatin(const char* word) {
      return con_PLatin(std::string(word)).c_str();
    }
    (Haskell:
    Code:
    import Data.List (break)
    import System (getArgs, getProgName)
    
    main = getArgs >>= doPigLatin
    
    doPigLatin [] = do progName <- getProgName
                       putStrLn $ "Usage: " ++ progName ++ " <word>"
    doPigLatin wrds = putStr . unlines . map pigLatin $ wrds
    
    pigLatin word = let (lead, rest) = break isVowel word
                     in rest ++ (if lead == "" then "way" else lead ++ "ay")
      where isVowel = (`elem` "aeiou")
    )

    Edit: surely there's some easy way to get rid of that annoying ‘const’ on the char* overload? Something like:
    Code:
    char *copy_string(const char* str) {
      char ret[strlen(str)];
      strcpy(ret, str);
      return ret;
    }
    ?

    Further edit: aha — I found it:
    Code:
    char* con_PLatin(const char* word) {
      return const_cast<char *>(con_PLatin(std::string(word)).c_str());
    }
    Since the resultant std::string is discarded, that should be safe, right?
    Last edited by Twey; April 10th, 2009 at 09:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center