• Register
278 points
6 1

Here we study about, a java program to reverse a string without using inbuilt function.

import java.util.Scanner;

public class ReverseString 
	public static void main(String args[])  
	     String s;  
         int i;
	     Scanner sc=new Scanner(System.in);  
	     System.out.print("Enter a String: ");  
	     s=sc.nextLine();     //reading string from user  
	     System.out.print("Reverse string is: ");  
	     for(i=s.length(); i>0; --i)     //i is the length of the string  
	          System.out.print(s.charAt(i-1));     //printing the character at index i-1  


Enter a String: Kodlogs is a site for learning different programming languages.
Reverse string is: .segaugnal gnimmargorp tnereffid gninrael rof etis a si sgoldoK

In the above code, I used for loop to reverse a string. The for loop executes until the condition i>0 becomes false. It prints the characters of the string which is at index (i-1) until i>0.

1 Comment

This article is really helpful. Thanks.