0

I have done a simple code to reverse a string in Java without using the inbuilt functions. But I have observed that unlike C where we can get a changed string back in the same variable using pointers, which it is not possible in Java due to the absence of pointer concept. So please show me what alternative way can I get back the string in the main function in Java.

class RevFun{
    public void revFun(StringBuilder str)
    {
        for(int i=str.length()-1;i>=0;i--)
        {
             System.out.println(str.charAt(i));//Here I am able to print it!
        }

        return;
    }
}

class Rev
{
    public static void main(String args[])
    {
        RevFun rev = new RevFun();
        StringBuilder str = new StringBuilder("Hello");
        System.out.println("Before reversing : "+str);
        rev.revFun(str);
        System.out.println("After reversing : "+str);//Here what should I do to get the reversed string from RevFun
    }
}
user85421
  • 28,957
  • 10
  • 64
  • 87
Ron Axm
  • 101
  • 9
  • First of all, you need to actually reverse it. Right now you just display it letter by letter starting from the end. Your `str` doesn't change. – Egan Wolf Sep 01 '17 at 11:22
  • And hint: you want us to spend our time to help you solve your problem. So you please spend the 1 minute it takes to properly align/format your code input, instead of dumping such an almost unreadable mess on us. – GhostCat Sep 01 '17 at 11:27
  • Two choices: A) your method returns a changed string B) you pass some your object within some *container* (for example an array or list) ... and then that function can update the container content. – GhostCat Sep 01 '17 at 11:29
  • 1
    The best way you can get updated string is by taking a variable, for instance, take String str2 in main() to get updated string and use the same for loop that you used in the revFun() to get reversed string. Inside the _for loop_ say 'str2 = str2+str1.charAt(i)' and print this str2 outside the for loop. This is how to can reverse string in Java. – Sanjana Jose Sep 01 '17 at 17:42
  • Thanks @SanjanaJose – Ron Axm Nov 08 '17 at 06:56

2 Answers2

0

give the second method returntype String

public static String handleString(String input){
  // modify input
  input += " test";
  return input;
}

and either use, or assign the (new) value in your main:

public static void main(String[] args){
  String a = "hello";
  String b = handleString(a);
  System.out.println(a);
  System.out.println(b);
}

another way is to have your variable on class level:

static String test = "hi";

public static void main(String[] args){
  System.out.println(test);
  handleString();
  System.out.println(test);
}

public static void handleString(){
  test += " and bye";
}

both methods have access to the variable, so you won't even need to pass it as a parameter.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

In Java Strings are immutable. This means that you can't change them. You can read more here

So if you want to "manipulate" an String you will have to return a new object

Alberto S.
  • 7,409
  • 6
  • 27
  • 46
  • this is not really what the question is about. It's about 'how to get the updated value'. it's basically about void vs returntype – Stultuske Sep 01 '17 at 11:27