reverse string in java

Discuss all aspects of programming here.

Moderator: The Mod Squad

reverse string in java

Postby cwl157 » Wed Nov 10, 2004 9:11 am

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
cwl157
Black Belt
Black Belt
 
Posts: 851
Joined: Mon Apr 12, 2004 2:32 pm
Location: Chicago

Postby Roach412 » Wed Nov 10, 2004 11:44 am

since it's an assignment, can't give you the whole thing. :( assignments, especially in programming, are there for you to learn to think through things yourself. 8)

here's some hints though: :D
.length() is your friend.
each character will need to be appended to the previous in order to output the final string correctly.(append is another reserved word)

just think about how you would need to break it down to do this by hand. and then move your process into "computer terms". one step at a time.

-Roach
Roach412
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2714
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin

Postby cwl157 » Wed Nov 10, 2004 2:33 pm

yea i was thinking of something with the .length() method like
Code: Select all
int i = 0;
while (i < s.length())
{
    s = s.charAt(i);  //like the character of the string at i
    i++;
}
 
return s;


i dont know how to store the difference characters i was thinking in an array but i dont know too much about them and also would i have to say like store length()-1 then length() -2 and so on until the whole string is done.
cwl157
Black Belt
Black Belt
 
Posts: 851
Joined: Mon Apr 12, 2004 2:32 pm
Location: Chicago

Postby Roach412 » Wed Nov 10, 2004 4:23 pm

try working through some psuedocode first. that also may help.

also, i would recommend trying to work it backwards...perhaps in a for loop or a do-while loop.

char x = selected character
string y = final reversed string

take length - 1 and set it to equal char x(since position 0 is first char)
append this char to a temporary string that comprises the final reversed string.
then just go through the loop until the position is < 0, which would be the end of the string.
then just output string y. :D

of course, the actual coding is missing here, but the idea will work...i've done it. :wink: not the most efficient (so you full-timers back off!) but it'll do what it needs.

-Roach
Roach412
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2714
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin

Postby cwl157 » Wed Nov 10, 2004 4:44 pm

Thanks for the help. I figured it out but now i need to make a method that takes a string and returns the string with no vowels. Any Ideas?
cwl157
Black Belt
Black Belt
 
Posts: 851
Joined: Mon Apr 12, 2004 2:32 pm
Location: Chicago

Postby Roach412 » Wed Nov 10, 2004 10:28 pm

array of characters(vowels).

compare each char to each of the spots in the array.
if it matches, don't put it in the returned string.

to be totally honest....you should be thinking these through by yourself.
these are the parts of programming which are the most important...because it's the basework. if you don't get these "easy" steps in figuring out small problems, the big ones just become overwhelming pretty quickly. :(

just my $.02

-Roach
Roach412
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2714
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin

Postby cwl157 » Thu Nov 11, 2004 1:41 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.
cwl157
Black Belt
Black Belt
 
Posts: 851
Joined: Mon Apr 12, 2004 2:32 pm
Location: Chicago

Postby Roach412 » Thu Nov 11, 2004 3:41 pm

:D i used to be an "if-else" ladder programmer....but i've kind of been weaned from my profs into one that uses arrays to compare multiple values quickly and efficiently. i haven't had a program yet that really "needed" super-efficient comparisons....but, better to be a little prepared. :wink:

gj w/ getting the prog to work how you wanted it.

-Roach
Roach412
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2714
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin

Postby tribaloverkill » Mon Nov 15, 2004 8:31 pm

I want to go to school for programming:( I cant wait to start writing code again.
tribaloverkill
Black Belt 1st Degree
Black Belt 1st Degree
 
Posts: 1032
Joined: Wed Sep 22, 2004 9:09 pm
Location: Mount Holly New Jersey

Postby tribaloverkill » Mon Nov 15, 2004 8:36 pm

I want to go to school for programming:( I cant wait to start writing code again.
tribaloverkill
Black Belt 1st Degree
Black Belt 1st Degree
 
Posts: 1032
Joined: Wed Sep 22, 2004 9:09 pm
Location: Mount Holly New Jersey

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest