CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2015
    Posts
    1

    How to turn String into Pig Latin

    Im trying to turn a string taken from the user into piglatin. I cannot use any special classes, methods, or arrays. I can only use a Scanner to create a object to take the string from the user and .length and .charAt, in addition to any type of looping. (Also cannot use switch statements or the break keyword)


    Here is an example of what my output is suppose to be:

    Enter a line of text: this is a test.
    Input : this is a test.
    Output: his-tay is-way a-way est-tay


    Here is my code, I can only get my code to work with one word and it must have a space at the end. Only one loop works at a time depending on the loop. Im not sure what to do if I get an entire String. Would you please mind showing me what I'm doing wrong? Thank you.

    Code:
    import java.util.*; 
    public class pL { 
    public static void main(String[] args) { 
    
    boolean space = false; 
    char firstChar; 
    
    System.out.print("Enter a line of text: "); 
    Scanner keyboard = new Scanner(System.in); 
    String text = keyboard.nextLine(); 
    
    System.out.println("\nInput: " + text); 
    System.out.print("Output: "); 
    
    /*for (int i = 0; i < text.length(); i++) { 
    
    if (((text.charAt(i)) == ('a')) || ((text.charAt(i)) == ('e')) 
    || ((text.charAt(i)) == ('i')) 
    || ((text.charAt(i)) == ('o')) 
    || ((text.charAt(i)) == ('u'))) { 
    
    System.out.print(text.charAt(i)); 
    } else if (text.charAt(i) == ' ') { 
    System.out.print("-way "); 
    } else 
    System.out.print(text.charAt(i)); 
    } 
    */ 
    /*for (int i = 0; i < text.length(); i++) { 
    firstChar = text.charAt(0); 
    if (text.charAt(i) == ' ') 
    System.out.print("-" + firstChar + "ay "); 
    else if (text.charAt(i) != text.charAt(0)) { 
    System.out.print(text.charAt(i)); 
    } 
    } 
    */ 
    } 
    }

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: How to turn String into Pig Latin

    Can you explain the logic in detail of how to convert a String of words into piglatin? You need to have the logic worked out before trying to write any code.

    Please edit your post and fix the code's indentation to make it easier to read and understand. All the indentation has been lost. Logically nested statements should be indented.
    Last edited by Norm; June 25th, 2015 at 07:55 PM.
    Norm

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to turn String into Pig Latin

    To handle the multiple words problem, just split the string by spaces. This will give you an array of individual words (where you can 'piglatin-ize' each word).

    Btw, simple pig latin rules are where you strip off a the leading consonances of a word up to the first vowel and then add those consonances to the end of the word and append an 'a' character. For words that begin with vowels, 'sa' is added to the end of the word. There are more complicated rules of piglatin that require you to apply the rule to each syllable, but solve for the simple case first.

    A couple of examples (using the simple rules):
    cat -> atca (pronounced at-kay)
    at -> atsa (pronounced at-say)
    Norm -> ormna (pronounced orm-nay)
    Programming -> ogrammingpa (pronounced o-gramming-pay)

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured