|
-
October 18th, 2006, 07:31 PM
#1
Help needed with code to scramble intermediate letters of a word
Hi all,
I am faced with a problem and was wondering if I could have assistance from a Java programmer of greater experience than myself.
I would like to make a java program that scrambles the intermediate letters of words in a string...
I would like each word in the string to consist of its original characters.
I would like the first and last letters unchanged and to retain the case in which they were written.
I would like the intermediate letters (excluding the frist and last characters) to be permuted randomly.
E.g The word "word" becomes "wrod".
The word "Programmer" becomes "Pogmmraer" or "Pmmrogarer".
Words can be arbitrarily short or long.
Any advice on even one part of the code would be GREATLY appreciated. Thanks so much.
-
October 18th, 2006, 09:23 PM
#2
Re: Help needed with code to scramble intermediate letters of a word
What part do you have so far, where are you stuck?
-
October 19th, 2006, 09:18 AM
#3
Re: Help needed with code to scramble intermediate letters of a word
I actually havent had a chance to attempt any of it yet.
-
October 19th, 2006, 10:56 AM
#4
Re: Help needed with code to scramble intermediate letters of a word
 Originally Posted by robo64
I actually havent had a chance to attempt any of it yet.
So how do you know you can't do it without help?
Learning how to learn is life's most important skill...
T. Buzan
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
October 19th, 2006, 12:06 PM
#5
Re: Help needed with code to scramble intermediate letters of a word
First of all it's not good practice to concatenate strings inside a loop, its better to use a StringBuilder object, for more information on how to and why read the bottom half of this Thread.
As for scrambling the letters I suggest you start by writing out how you would do it if you had each letter on a separate peice of paper and you are starting with the letters aligned left to right in the correct order to spell out the word. Once you've worked out a strategy to acheive your goal the writing of the code will be easier. If you get stuck again post your strategy and latest code and we'll help further.
-
October 19th, 2006, 06:02 PM
#6
Re: Help needed with code to scramble intermediate letters of a word
Thanks so much for your help. I'll get back to you with my solution to this, so that other people can use it... pretty useless, but fun!
-
October 19th, 2006, 07:23 PM
#7
Re: Help needed with code to scramble intermediate letters of a word
I hope this code is more acceptable, I am almost there and I am able to scramble the word. I just can't seem to keep the first and last letters in place. Any ideas?
{
//-----------------------------------------------------------------
// Changes text, so that the intermediate letters are scrambled.
//-----------------------------------------------------------------
public static String translate (String sentence)
{
String result = "";
Scanner scan = new Scanner (sentence);
while (scan.hasNext())
{
result += translateWord (scan.next());
result += " ";
}
return result;
}
//-----------------------------------------------------------------
// Translates one word into scrambled text. If the word has two or
// less letters, the word is left the same. Otherwise,
// the first letter and last letter are left the same and the,
// intermediate letters are scrambled.
//-----------------------------------------------------------------
public static String translateWord (String word)
{
int j = 0;
String scrambled = "";
java.util.Random r;
r = new java.util.Random();
for (j = 0; j <word.length(); j++)
{
if ( r.nextBoolean() )
scrambled = scrambled + word.charAt(j);
else
scrambled = word.charAt(j) + scrambled;
}
return scrambled;
}
-
October 19th, 2006, 09:25 PM
#8
Re: Help needed with code to scramble intermediate letters of a word
So you have a method that will scramble letters. Cool. But you don't want to pass it the whole word. You only want to pass it that part of the word that you need scrambled.
Code:
char firstChar = extract first char; //How would you do this?
char lastChar = extract last char; //See above.
String middlePortion = get a String that has all chars except the first and last;
//Hint see String.substring()
String myResult = firstChar + translateWord(middlePortion) + lastChar;
return myResult;
Take a look at the methods of java.lang.String class and try to see what methods you could use to extract the first and last chars of a String.
Last edited by gmrowe1075; October 19th, 2006 at 09:30 PM.
-
October 20th, 2006, 01:17 AM
#9
Re: Help needed with code to scramble intermediate letters of a word
Gmrowe, thanks for the tips. I've played with my code for about half hour, but can't seem to work anything out. You gave me a tip to look at string.substring... what did you mean by that? Thanks so much.
-
October 20th, 2006, 03:52 AM
#10
Re: Help needed with code to scramble intermediate letters of a word
Alternatively, you could take another look at your scrambling loop. The first and last characters are being included because your loop starts at the first character and finishes at the last. If you start at the second character (index 1) and finish at the penultimate character (index length-2), you won't scramble the first and last characters. You might also want to check for words less than 4 characters long - you can't scramble them!
A man's reach should exceed his grasp, or what's heaven for?
R. Browning
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
October 20th, 2006, 09:55 AM
#11
Re: Help needed with code to scramble intermediate letters of a word
Take a look at Collections.shuffle() method.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
October 20th, 2006, 11:55 AM
#12
Re: Help needed with code to scramble intermediate letters of a word
dlorde, could you give me and example of this please? I am just not very good at Java, but would love to accomplish this task. I have now spent hours trying random things and I know that you would probably find this much easier...
Here is my method
{
int a = 0;
int b = word.length() -1;
String scrambled = "";
java.util.Random r;
r = new java.util.Random();
for (a = 0; a <word.length(); a++)
{
if ( r.nextBoolean() )
scrambled = scrambled + word.charAt(a);
else
scrambled = word.charAt(a) + scrambled;
}
return scrambled;
}
Where do I put those paramaters in? I tried up at the top in the variables, but that doesn't seem to work. I also tried it inside the for statement and then within the loop. Where am i going wrong? Please help.
-
October 20th, 2006, 12:19 PM
#13
Re: Help needed with code to scramble intermediate letters of a word
As dlorde said the for loop needs to iterate from the second letter to the one from last letter so it needs to be:
Code:
for (j = 1; j <word.length()-1; j++)
This will build a scrambled string containing just the middle letters so you then need to add the first and last letter before returning the result:
Code:
return word.charAt(0)+scrambled+word.charAt(word.length()-1);
As dlorde also pointed out you also have to test for words containing less than 4 characters and just return them unscrambled.
It's also more efficient to assigned word.length()-1 to another variable and use that (as you attempted to do in your last posted code).
-
October 20th, 2006, 12:26 PM
#14
Re: Help needed with code to scramble intermediate letters of a word
Ok I did this without the IF/ELSE in the middle and I am able to return the first and last letters only. I am not able to return the scrambled letters in the middle of the word... they magically disappear. Any ideas?
Last edited by robo64; October 20th, 2006 at 12:42 PM.
-
October 20th, 2006, 05:07 PM
#15
Re: Help needed with code to scramble intermediate letters of a word
Well if you removed the if/else clause then you would only get the first and last letters because the variable "scrambled" will be an empty string. The last line will then be concatenating the first letter + an empty string + the last letter.
I've just noticed that from your first posted code to the most recent posted code you've changed the for-loop variable from 'j' to 'a'. My last code suggestion used 'j' so you'll either have to change it to 'a' or change the use of 'a' within the for-loop to 'j'.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|