Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Reversing character and Words in a String.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
a22asin




PostPosted: Sat Nov 30, 2013 11:21 am   Post subject: Reversing character and Words in a String.

HI, im having a bit of trouble with this assignment where i have to reverse the characters in a word as well as the word itself in a string given by the user. The hard part about it is that i have to do it using a recursive method. Im new to java, and having quite a bit of trouble adjusting to it. lol. Anywho, i managed to get it to reverse the characters in the 1st string, but not in the whole string, and nor could i manage to reverse the ordering of the words (ie. I love comp sci = ics evol I). Thi is what i got:

code:

  public static String reverseWord(String s) {
                        if (s.length() == 1) {
                                return s;
                        }else {
                                char c = s.charAt(0);
                                return (s.substring(1)) + c;
                        }
        }
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Sat Nov 30, 2013 11:46 am   Post subject: RE:Reversing character and Words in a String.

First of all, it looks like this will work if you have a 1 or 2 letter word. However, the thing is that you are only going through this once.

Eg. s = "hi"

it puts the h at the end and puts the i first, so "ih"

Eg. s = "hello"

It puts the h at the end, but now it will put "ello" in front of it. So, "elloh"

Ever Have you ever used resursion? You're very, very close to solving it.
a22asin




PostPosted: Sat Nov 30, 2013 12:16 pm   Post subject: RE:Reversing character and Words in a String.

No i have never used recursion. My code is based on some research and wha i learned in class. But it has only taken me this far, from here i have no idea what to do unfortunately.
crossley7




PostPosted: Sat Nov 30, 2013 12:55 pm   Post subject: RE:Reversing character and Words in a String.

By definition, a recursive function will call itself. You are extremely close with your code there. Just find what you need to call the function with to get it to continue reversing the rest of the string.
Raknarg




PostPosted: Sat Nov 30, 2013 1:05 pm   Post subject: RE:Reversing character and Words in a String.

Well, I'll give you a simple example of recursion. An easy example would be factorials.

2! = 1x2
4! = 1x2x3x4
9! = 1x2x3x4x5x6x7x8x9

and so on.
Java:

public static int factorial(int n) {
   if (n <= 1) {
      return 1;
   }
   else {
      return n * factorial(n - 1);
   }
}


Look what happens if I call factorial(3): It says "ok, 3 is greater than 1 so return 3 * factorial(2)". It does the same thing with 2, returning 2*factorial(1). Factorial(1) is 1, so 2 * 1 is 2, 3 * 2 is 6. Therefore, factorial(3) gives you 6.
a22asin




PostPosted: Sat Nov 30, 2013 1:30 pm   Post subject: RE:Reversing character and Words in a String.

ok, so i have to make the program repeat for each word, i tried to use the split() command, but had no luck getting it to repeat. I cant seem to figure this out. his is truly frustrating lol.
Raknarg




PostPosted: Sat Nov 30, 2013 2:58 pm   Post subject: RE:Reversing character and Words in a String.

Ok,so if you're given the string "hello world" does it need to be "olleh dlrow" or "dlrow olleh"?
a22asin




PostPosted: Sat Nov 30, 2013 3:22 pm   Post subject: RE:Reversing character and Words in a String.

it would be "dlrow olleh"
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Sat Nov 30, 2013 4:44 pm   Post subject: RE:Reversing character and Words in a String.

Alright, think about what your code is down. If you want to reverse the word, you can put the first letter at the end, but you want the rest of the string inverted as well right?

You want to add (the inverse of the string without the first letter) to (the first letter)
a22asin




PostPosted: Sat Nov 30, 2013 4:53 pm   Post subject: RE:Reversing character and Words in a String.

ok.... but how would i implement that into my code?
Raknarg




PostPosted: Sat Nov 30, 2013 11:07 pm   Post subject: RE:Reversing character and Words in a String.

Think about it! You wrote a piece of code that can reverse a string for you. You want to return the restof the string reversed plus the first letter.

Remember how I showed you the factorial thing? I called factorial(n-1) inside the program, because I wrote a piece of code that already found factorials for me.

The key here is that the funcion reverseWord already reverses stuff for you.

Try to put these ideas together

(I usually try to not give straight answers. I'll tell you, but I want you to think about it hard before you get an answer)
a22asin




PostPosted: Sun Dec 01, 2013 10:22 am   Post subject: Re: Reversing character and Words in a String.

Yay, i go it! There was a glitch with my scanner object that i used to get input. Somehow it didnt read the string properly, so much of my previous code worked. But i fixed it and now the code works!! Heres wat i got:

code:

public static String reverseWord(String word) {

                if (word.length() <= 1) {
                        return word;
                }else{
                        return reverseWord(word.substring(1)) + word.charAt(0);
                }
        }
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: