473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

String Manipulation - English to pig Latin

1
I'm creating a program to convert a .dat file from English to pig Latin under the following rules:

1. If the word begins with a vowel, add "-way" to the end.

2.If the word doesn't begin with a vowel, add a "-" to the end, then put the first letter after the "-" and so on until the first letter of the word is a vowel, then add "ay" to the end after the letters you added.

3. y is considered a vowel

4.If a word has no vowels at all, just add "-way" to the end.

5. Punctuation goes at the end after the word has been converted. (i.e. "Hello:" ---> "ello-Hay:")

I'm having trouble with #5, here's what I have so far: (Any help is very much appreciated!)

Expand|Select|Wrap|Line Numbers
  1. import java.lang.*;
  2. import java.util.regex.*;
  3. import java.io.*;
  4.  
  5. public class prog5 {
  6.     public static void main (String[] args) throws IOException{
  7.  
  8.         FileInputStream fis = new FileInputStream(new File("prog5.dat"));
  9.         InputStreamReader isr = new InputStreamReader(fis);
  10.         char[] text = new char[fis.available()];
  11.  
  12.         isr.read(text, 0, fis.available());
  13.         isr.close();
  14.  
  15.         String data = new String(text);
  16.  
  17.         String[] para = data.split("\r\n\r\n");
  18.  
  19.         String p1 = para[0];
  20.         String[] wordOne = p1.split("\\s");
  21.  
  22.         for (int i = 0; i < wordOne.length; ++i) {
  23.  
  24.             if (wordOne[i].equals(""))
  25.                 ++i;
  26.             else {
  27.                 String str = wordOne[i];
  28.  
  29.                 convert(str);
  30.                 wordOne[i] = str;
  31.                 System.out.flush();
  32.             }
  33.  
  34.         }
  35.  
  36.         System.out.print("\n\n");
  37.  
  38.         String p2 = para[1];
  39.         String[] wordTwo = p2.split("\\s");
  40.         for (int i = 0; i < wordTwo.length; ++i) {
  41.  
  42.             if (wordTwo[i].equals(""))
  43.                 ++i;
  44.             else {
  45.                 String str = wordTwo[i];
  46.  
  47.                 convert(str);
  48.                 wordTwo[i] = str;
  49.                 System.out.flush();
  50.  
  51.             }    
  52.         }        
  53.  
  54.         String p3 = para[2];
  55.  
  56.         String[] wordThree = p3.split("//s");
  57.         for (int i = 0; i < wordThree.length; ++i) {
  58.  
  59.             if (wordThree[i].equals(""))
  60.                 ++i;
  61.             else {
  62.                 String str = wordThree[i];
  63.  
  64.                 convert(str);
  65.                 wordThree[i] = str;
  66.                 System.out.flush();
  67.  
  68.             }    
  69.         }    
  70.  
  71. }
  72.  
  73. public static boolean isVowel(char ch) {
  74.  
  75.     String VOWELS = "aeiouyAEIOUY";
  76.     return (VOWELS.indexOf(ch) != -1);
  77.  
  78. }
  79.  
  80. /*public static int firstVowel(String word) {
  81.     // declare and intiialize counter variable
  82.  
  83.     int i = 0; // loop through the characters in a word
  84.  
  85.     while (i < word.length()) {
  86.         if (isVowel(word.charAt(i)))
  87.             return i;
  88.  
  89.         i++;
  90.     }
  91.     return (-1);
  92. }*/
  93.  
  94. public static String rotate(String s) {
  95.      int len = s.length();
  96.  
  97.      String rStr;
  98.  
  99.      rStr = s.substring(1,len) + s.charAt(0);
  100.  
  101.      return rStr;
  102. }
  103.  
  104. public static String convert(String s) {
  105.  
  106.     int len;
  107.     //StringBuffer result = new StringBuffer(s);
  108.     boolean foundVowel;
  109.  
  110.     if(isVowel(s.charAt(0))) {
  111.         s = s + "-way";
  112.         //System.out.print(s + " ");
  113.     }else {
  114.  
  115.         s = s + "-";
  116.         s = rotate(s) + "ay";
  117.  
  118.         len = s.length();                                    
  119.         foundVowel = false;                                       
  120.  
  121.         for(int i = 1; i < len - 1; ++i) {
  122.  
  123.             if(isVowel(s.charAt(0))) {
  124.                 foundVowel = true;
  125.                 break;
  126.             } else                                                    
  127.                 s = rotate(s);
  128.  
  129.             if(!foundVowel)                                           
  130.                 s = s.substring(1,len) + "-way";
  131.             else
  132.                 s = s + "ay";
  133.         }
  134.     }
  135.     System.out.print(s + " ");
  136.     return s;
  137.     }
  138.  
  139.  
  140. }
  141.  
Mar 19 '07 #1
3 3437
DeMan
1,806 1GB
you might like to first check whether there are any vowels in the phrase (after all you have a method to do this - is there a rpoblem with it, that is't commented out?). Then if that method return 0 or -1, add way to the end. Otherwise make a string of the substring from the return to the end + '-' +the begining of the string + ay
Somethign along the lines of:
Expand|Select|Wrap|Line Numbers
  1. int firstVowel = firstVowel(inpoutString);
  2. if((firstVowel == -1) || (firstVowel == 0))
  3. {
  4.   return (inputString + "way");
  5. }
  6. else
  7. {
  8.   return (inputString.substring(firstVowel, inputString.length()-1) + "-" + inputString.substring(0, firstVowel-1) + "ay");
  9. }
  10.  
you may like to manipulate the strings before returning them (just to add clarity to what is going on....)
Mar 19 '07 #2
Ganon11
3,652 Expert 2GB
Although DeMan's advice is valid, you had questions about number 5 - specifically, the punctuation.

I would write a method similar to isVowel called isPunctuation (or isPunct, or any short name that is understandable). Then, when you have the word (such as "Hello."), check the last character to see if it is punctuation. If it is, remove it from the word and continue computing normally.
Mar 20 '07 #3
r035198x
13,262 8TB
Although DeMan's advice is valid, you had questions about number 5 - specifically, the punctuation.

I would write a method similar to isVowel called isPunctuation (or isPunct, or any short name that is understandable). Then, when you have the word (such as "Hello."), check the last character to see if it is punctuation. If it is, remove it from the word and continue computing normally.
Yeah, if you have any punctuation just pluck it out using substring, manipulate the remaining word, then add the punctuation back after the conversion.

You've been doing a good job of it, may I add.
Mar 20 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: mjakowlew | last post by:
Hi, I'm trying to use some string manipulation from a file's path. filepath='c:\documents\web\zope\file.ext' I need to extract everthing after the last '\' and save it. I've looked around...
4
by: Jim McGivney | last post by:
Does anyone know of a concise article that covers string manipulation, such as insert, join, pad, etc. Thanks, Jim
7
by: John A Grandy | last post by:
what are the preferred VB.NET analogues for IsNumeric() and Len() and CInt() & similar string-manipulation functions in VB6
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
5
by: Cleverbum | last post by:
I'm not really accustomed to string manipulation and so I was wondering if any of you could be any help i speeding up this script intended to change the format of some saved log information into a...
5
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
3
by: crprajan | last post by:
String Manipulation: Given a string like “This is a string”, I want to remove all single characters( alphabets and numerals) like (a, b, 1, 2, .. ) . So the output of the string will be “This is...
3
by: frankeljw | last post by:
I have 2 Java strings 1st String is a series of names, colons, and numbers ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:Name6:44 2nd String is a name ie) Name2 I need to get the...
1
by: adam bob | last post by:
Hello, I'm struggling with an image mechanism I'm trying to build which basically manipulates a URL string. This is the sort URL that is gained from an InfoPath form ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.