Program to reverse a sentence using recursion concept
Java program to reverse a sentence using recursion
public class Reverse {
public static void main(String[] args) {
String sentence = "Go work";
String reversed = reverse(sentence);
System.out.println("The reversed sentence is: " + reversed);
}
public static String reverse(String sentence)
{
if (sentence.isEmpty())
return sentence;
return reverse(sentence.substring(1)) + sentence.charAt(0);
}
}
public class Reverse {
public static void main(String[] args) {
String sentence = "Go work";
String reversed = reverse(sentence);
System.out.println("The reversed sentence is: " + reversed);
}
public static String reverse(String sentence)
{
if (sentence.isEmpty())
return sentence;
return reverse(sentence.substring(1)) + sentence.charAt(0);
}
}
Comments
Post a Comment