Java Program to Reverse Letters in a String

Write a Java program to reverse individual word letters in a string using for loop. First, we use the Java string split function to split the given string into individual words. Then, within the for loop, we assign each word to a character array. Next, we used another for loop to iterate each string word from last to first to print them in reverse order.

package SimpleNumberPrograms;
import java.util.Scanner;

public class StringLettersReverse {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		String revStrWords;

		sc= new Scanner(System.in);
		
		System.out.print("Enter String to Reverse its Letters =  ");
		revStrWords = sc.nextLine();
		
		String[] strArray = revStrWords.split(" ");
		
		for(int i = 0; i < strArray.length; i++) 
		{
			char[] ch = strArray[i].toCharArray();
			for(int j = ch.length - 1; j >= 0; j--) 
			{
				System.out.print(ch[j]);
			}
			System.out.print(" ");
		}
	}
}
Java Program to Reverse Letters in a String 1

In this Java reverse letters in a string example, we replaced the for loop with a while loop.

package SimpleNumberPrograms;
import java.util.Scanner;

public class StringLettersReverse2 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		String revStrWords;
		int j, i = 0; 
		
		sc= new Scanner(System.in);
		
		System.out.print("Enter String to Reverse its Letters =  ");
		revStrWords = sc.nextLine();
		
		String[] strArray = revStrWords.split(" ");
		
		while(i < strArray.length) 
		{
			char[] ch = strArray[i].toCharArray();
			j = ch.length - 1;
			while( j >= 0) 
			{
				System.out.print(ch[j]);
				j--;
			}
			System.out.print(" ");
			i++;
		}
	}
}
Enter String to Reverse its Letters =  Java Programs
avaJ smargorP 

Java Program to Reverse Letters in a String using do while loop.

package SimpleNumberPrograms;
import java.util.Scanner;

public class StringLettersReverse3 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		String revStrWords;
		int j, i = 0; 
		
		sc= new Scanner(System.in);
		
		System.out.print("Enter String to Reverse its Letters =  ");
		revStrWords = sc.nextLine();
		
		String[] strArray = revStrWords.split(" ");
		
		do
		{
			char[] ch = strArray[i].toCharArray();
			j = ch.length - 1;
			
			do
			{
				System.out.print(ch[j]);
			} while( --j >= 0);
			System.out.print(" ");
		} while(++i < strArray.length);
	}
}
Enter String to Reverse its Letters =  Learn Java Programming for Free!
nraeL avaJ gnimmargorP rof !eerF