cwl157
11-10-2004, 05:22 PM
for my java class i need to write a method that would take a string the user inputs and output the characters in reverse order. Any ideas on how to do this cause i have no clue. Thanks
|
||||
reverse string in javacwl157 11-10-2004, 05:22 PM for my java class i need to write a method that would take a string the user inputs and output the characters in reverse order. Any ideas on how to do this cause i have no clue. Thanks whackaxe 11-10-2004, 06:10 PM i don't know java, but the baisc process woulmd be this -make an array the size of the length of your string -make a for loop iterating up(++) to the strings length (lets call it i) -take originalString[i] and place it in newString[originalString-i] that should do it Antoniohawk 11-10-2004, 06:40 PM Yup, do what Whackaxe suggested, or you could always just use charAt() and make a String instead of an array. Haven't tried it, but it should work. KeZZeR 11-10-2004, 08:14 PM You could do it without the array, i'd write it like this in javascript, would be damn similar in java: var result; result = ''; for (var position = 0; position < aString.length; position = position + 1) { result = aString.charAt(position) + result }; return result Spookster 11-10-2004, 11:45 PM You could do it without the array, i'd write it like this in javascript, would be damn similar in java: var result; result = ''; for (var position = 0; position < aString.length; position = position + 1) { result = aString.charAt(position) + result }; return result You realize you just did his homework for him right? cwl157 11-11-2004, 12:52 AM Thanks a lot for the help ive been working on this all week and i couldnt figure it out. So now my next problem is i have to make a method that takes a string and removes the vowels then returns the string. Any Ideas? turbowrx 11-11-2004, 02:00 AM Couple ways to do this, but I would recommend using the stringbuffer. These objects are made for modifying strings. Just create an array of vowel characters and make your comparisons. Loop through the stringbuffer and check to see if the character matches any index in the vowel array, if yes, delete it, if no, move on. Later. squirellplaying 11-11-2004, 11:40 AM Loop through the string changing each character to an int. Then check using the dec. value (http://asciitable.com) it returns. It's easier if you convert it to all lower case. If the character does not fall into one of the ranges you set, add it to a new string. That's probably more of the way they want it done. If you want it done quick and easy look through here. (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html) KeZZeR 11-11-2004, 12:57 PM Thanks a lot for the help ive been working on this all week and i couldnt figure it out. So now my next problem is i have to make a method that takes a string and removes the vowels then returns the string. Any Ideas? lol, i had to do the exact same thing back in my old course. What/where are you doing this for study? cwl157 11-11-2004, 07:16 PM thos 2 solutions sound a little complicated for my current level? squirellplaying 11-11-2004, 09:02 PM It doesn't get any easier then that. Read the second link I gave you. That is the similest that it gets. cwl157 11-11-2004, 09:47 PM i figured out the program and everything works to remove the vowels i just used an if statement that compared each char in the string to every different vowel and if they were not equal then it added that char to the final string. |
| |||
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum