Core JAVA
Java Runtime Environment (JRE)
Java Virtual Machine (JVM)
Java overview
Java basics
Java Objects and classes
Java Constructors
Java basic datatypes
Java variable types
Java modifiers/Access Modifiers In Java
Java Basic Operators
Java Loops and Controls
Java conditions
Java numbers and characters
Java strings
Java arrays
Java date time
Java methods
Java file and IO operations
Java exceptions
Inner class
Java OOPs Concepts
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java Interface
Cohesion and Coupling
Association, Aggregation and Composition
Java Collections
Java ArrayList
Java LinkedList
Set and HashSet
LinkedHashSet and TreeSet
Queue and PriorityQueue
Deque and PriorityQueue
Java Map Interface
Java HashMap
Internal Working Of Java HashMap
Java Mutithread
Methods of Thread In Java
Join , run & Start Method in Threads
Difference b/w start & run Methods in Threads
Java Concurrency Package & its Features
CountDownLatch, CyclicBarrier, Semaphore and Mutex in Thread
Print Words Of String In Reverse Order
In this java program we will develop a solution to print words of string in reverse order.
Java Program To Print Words Of String In Reverse Order
package problems;public class StringWordsInReverse {
public static String reverseTheWordsSol1(String input) {
String result = "";
String arr[] = input.split(" ");
int i = arr.length -1;
while(i >=0){
if(i == arr.length -1)
result = result + arr[i];
else
result = result + " " + arr[i];
i--;
}
return result;
}public static String reverseTheWordsSol2(String input) {
int i = input.length() - 1;
int start, end = i + 1;
String result = "";
while (i >= 0) {
if (input.charAt(i) == ' ') {
start = i + 1;
while (start != end)
result += input.charAt(start++);
result += ' ';
end = i;
}
i--;
}start = 0;
while (start != end)
result += input.charAt(start++);
return result;
}public static void main(String[] args) {
System.out.println("Reversed words in string 'I am Alex' is: "+reverseTheWordsSol1("I am Alex"));
System.out.println("Reversed words in string 'I am Alex' is: "+reverseTheWordsSol2("I am Alex"));
}
}
Output:
Reversed words in string 'I am Alex' is: Alex am I
Reversed words in string 'I am Alex' is: Alex am I