Wednesday, 11 June 2025

Backward String Java Program

Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display *yrivaig*. Demonstrate the method in a program that asks the user to input a string and then passes it to the method.


Code:

package programmingChallenges;
import java.util.*;
public class BackwardString {
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
String str,str2;
char[] ch;
int i;
System.out.print("Enter String: ");
str = sc.nextLine();

ch = str.toCharArray();

for(i=str.length()-1;i>=0;i--)
{
System.out.print(ch[i]);
}
}
}



No comments:

Post a Comment