Results 1 to 10 of 10
- 04-18-2010, 10:54 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
copying a part of a string and moving it to the end of the string?
If I have a String name "sentence" which contained the words: "Translate the string to Pig Latin"
How would I select the consonants or every letter before a vowel and move it behind the word, like this: (the apostrophe and caps are added to shows the letters i moved to the back of the word)
sentence = anslate'TR' e'TH' int'ST' o'T' ig'P' atin'L'
I was thinking about putting it into an array of words
-
How about creating a method
and then loop through each word's characters until a vowel is reached.Java Code:public static boolean isVowel(char c) { // code goes here to see if c is a vowel. }
- 04-19-2010, 06:42 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
I already have a main
And this is my code:Java Code:package package10; import javax.swing.JOptionPane; public class XlatorMain { /** * @param args */ public static void main(String[] args) { // if you don't have one (or more) of these translators implemented, jut comment out the line int [] translator = { Xlator.UNCHANGED, Xlator.LOWERCASE, Xlator.UPPERCASE, Xlator.ALTERNATE, Xlator.PIGLATIN, //Xlator.SWEDISHCHEF }; String input = JOptionPane.showInputDialog(null,"Enter the text for translation: "); while (input!= null) { String outputString = ""; for (int j = 0; j < translator.length; j++) { String ans1 = Xlator.Xlate(translator[j], input); outputString = outputString + "\n'" + input + "' translates to '" + ans1 + "' using method " + translator[j]; } input = JOptionPane.showInputDialog(null,outputString + "\n\nEnter the text for translation: "); } } }
I want to getJava Code:package package10; public class Xlator { public static final int UNCHANGED = 0; public static final int LOWERCASE = 1; public static final int UPPERCASE = 2; public static final int ALTERNATE = 3; public static final int PIGLATIN = 4; public static String getAuthor(){ return "Sid Patel"; } public static String Xlate(int translator, String sentence){ if (translator == 1){ return sentence.toLowerCase(); } else if (translator == 2){ return sentence.toUpperCase(); } else if (translator == 3){ for (int i = 0; i < sentence.length(); i++){ sentence = Character.toUpperCase(sentence.charAt(i)) + sentence.substring(1); } return sentence; } else return sentence; } }working so that it will return the characters in an alternating case. for example "CaSe". I got the first letter working, but don't know how to loop it so that all the letters will workJava Code:else if (translator == 3){ for (int i = 0; i < sentence.length(); i++){ sentence = Character.toUpperCase(sentence.charAt(i)) + sentence.substring(1); } return sentence;
- 04-19-2010, 08:51 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 88
- Rep Power
- 0
if(1 % 2 == 2) simply finds if the remainder of i / 2 = 0, in other words, finds if it is an even character, and this line that swaps the character will be skipped for odd values of i.Java Code:for (int i = 0 ; i < sentence.length () ; i++) { if (i % 2 == 0) { sentence = sentence.substring (0, i) + Character.toUpperCase (sentence.charAt (i)) + sentence.substring (i + 1, sentence.length ()); } } return sentence;
By the way, have you just given up on the igpay atinlay ?
Cheers.Last edited by Cruncher; 04-19-2010 at 08:54 AM.
- 04-19-2010, 09:32 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Do this right and use a StringBuilder, though. You may as well learn good habits when you're starting, rather than having to unlearn bad habits later.
-Gary-Java Code:StringBuilder sb = new StringBuilder(sentence.length()); int i = 0; for (char c : sentence.toCharArray()) { if (i++ % 2 == 0) { sb.append(Character.toUpperCase(c)); } else { sb.append(Character.toLowerCase(c)); } } sentence = sb.toString();
- 04-19-2010, 11:15 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
Thanks, it definitely works great and nope, I haven't forgotten that Pig Latin translator part, just haven't gotten to it yet.
I do have a quick question though. What makes:
Originally posted by CruncherJava Code:for (int i = 0 ; i < sentence.length () ; i++) { if (i % 2 == 0) { sentence = sentence.substring (0, i) + Character.toUpperCase (sentence.charAt (i)) + sentence.substring (i + 1, sentence.length ()); } } return sentence;
and this one here better if they accomplish the same thing in different ways:
Originally posted by gcalvinJava Code:StringBuilder sb = new StringBuilder(sentence.length()); int i = 0; for (char c : sentence.toCharArray()) { if (i++ % 2 == 0) { sb.append(Character.toUpperCase(c)); } else { sb.append(Character.toLowerCase(c)); } } sentence = sb.toString();Last edited by sidd0123; 04-20-2010 at 04:03 AM.
- 04-20-2010, 04:13 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
It's a little complicated to explain the differences in efficiency. The first approach seems to be updating the String "in place" but remember that Java Strings are immutable, which means that substring() has to create a new String at each invocation and you're building a new String sentence each time through the loop. Also, appending Strings with + is considerably less efficient than StringBuilder's append() method. Not that efficiency should be your first concern when you're learning, but it's just a good habit when you're doing a lot of appending on a String to use a StringBuilder instead.
(Also, the first version leaves the character unchanged if it's an odd position, while the second version makes sure it's lower case.)
-Gary-
- 04-20-2010, 08:12 AM #8
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
I used the following code and it fixes the issue :
(Also, the first version leaves the character unchanged if it's an odd position, while the second version makes sure it's lower case.):)Java Code:for (int i = 0 ; i < sentence.length () ; i++) { if (i % 2 == 0) { sentence = sentence.substring (0, i) + Character.toUpperCase (sentence.charAt (i)) + sentence.substring (i + 1, sentence.length ()); } if (i % 2 != 0) { sentence = sentence.substring (0, i) + Character.toLowerCase(sentence.charAt (i)) + sentence.substring (i + 1, sentence.length ()); } } return sentence;
For the pig latin translator, could I possibly use another substring and remove the consonants before the vowel using a method from the characters class file form Java docs?
- 04-21-2010, 08:33 PM #9
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
Anyone here???
- 04-23-2010, 01:52 AM #10
Member
- Join Date
- Mar 2010
- Posts
- 88
- Rep Power
- 0
Okay i'm not going to code this i'll explain this is pseudo, if you have problems with it then we'll talk code.
basically, you need to loop through each character of the word until you hit vowel. so you will need a method that should probably return a boolean, and pass it a character, this method should return true if it's a vowel and false if it's not. so just break out of the loop when the ith character is a vowel and then move everything before it to the back using substring.
If you need help with that, post back.
Similar Threads
-
unicode as part of string
By mac in forum New To JavaReplies: 0Last Post: 01-28-2010, 02:23 AM -
search with part of string
By virendra in forum LuceneReplies: 1Last Post: 01-21-2010, 12:56 PM -
copying part of a BufferedImage into a Graphics
By flok in forum AWT / SwingReplies: 1Last Post: 11-26-2009, 05:05 PM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
How to get part of a String?
By eva in forum New To JavaReplies: 1Last Post: 12-23-2007, 06:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks