-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReverseWordExample.java
More file actions
27 lines (25 loc) · 952 Bytes
/
ReverseWordExample.java
File metadata and controls
27 lines (25 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.codinginterview;
import java.util.Scanner;
public class ReverseWordExample {
public static void main(String[] args) {
System.out.println("Reverse a Word from the Sentence Example");
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String ::");
String str = sc.next();
System.out.println("String Before ::"+ str);
System.out.println("String After ::"+ reverseWord1(str));
}
private static String reverseWord1(String sentance){
sentance = "Dharmendra Kumar Sahu";
String[] strArr = sentance.split(" ");
String reverseString = "";
for (String str : strArr){
String reverseWord = "";
for(int i = str.length() - 1; i>=0; i--){
reverseWord = reverseWord + str.charAt(i);
}
reverseString = reverseString + reverseWord + " ";
}
return reverseString;
}
}