Printing reverse of any String without using any predefined function?

后端 未结 30 1322
生来不讨喜
生来不讨喜 2020-11-30 03:08

How to print the reverse of the String java is object orientated language without using any predefined function like reverse()?

30条回答
  •  遥遥无期
    2020-11-30 03:20

    public String reverse(String arg)
    {
        String tmp = null;
        if (arg.length() == 1)
        {
            return arg;
        }
    
        else
        {
    
            String lastChar = arg.substring(arg.length()-1,arg.length());
    
            String remainingString = arg.substring(0, arg.length() -1);
    
            tmp = lastChar + reverse(remainingString);
            return tmp;
    
    
        }
    }
    

提交回复
热议问题