Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

reverse sentence in java

// INPUT: "you shall not pass"
// OUTPUT: "pass not shall you"
  
String s[] = "you shall not pass".split(" "); 
String ans = ""; 
for (int i = s.length - 1; i >= 0; i--) {
  ans += s[i] + " ";
}
System.out.println(ans);
Comment

reverse words in a string java

// Java Program to reverse a String
// without using inbuilt String function
import java.util.regex.Pattern;
public class Exp {
 
    // Method to reverse words of a String
    static String reverseWords(String str)
    {
 
        // Specifying the pattern to be searched
        Pattern pattern = Pattern.compile("s");
 
        // splitting String str with a pattern
        // (i.e )splitting the string whenever their
        // is whitespace and store in temp array.
        String[] temp = pattern.split(str);
        String result = "";
 
        // Iterate over the temp array and store
        // the string in reverse order.
        for (int i = 0; i < temp.length; i++) {
            if (i == temp.length - 1)
                result = temp[i] + result;
            else
                result = " " + temp[i] + result;
        }
        return result;
    }
 
    // Driver methods to test above method
    public static void main(String[] args)
    {
        String s1 = "Welcome to geeksforgeeks";
        System.out.println(reverseWords(s1));
 
        String s2 = "I love Java Programming";
        System.out.println(reverseWords(s2));
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: read file in java 
Java :: read double java 
Java :: even digit sum java 
Java :: Caused by: java.lang.NoSuchMethodError: org.apache.logging.log4j.spi.LoggerContextFactory.isClassLoaderDependent()Z 
Java :: return day from date in java 
Java :: no main manifest attribute java 
Java :: create file from byte array java 
Java :: print to console in java 
Java :: char equals java 
Java :: foreach map java 
Java :: java save file 
Java :: how to destroy activity in android 
Java :: java cheat sheet 
Java :: how to count an replace substring string in java 
Java :: java create new thread 
Java :: add seconds to today date and time java 
Java :: java create file and parent directories 
Java :: java program to print 1 to 100 using for loop 
Java :: convert list of integer to array in java 
Java :: converting excel to csv in java 
Java :: android use attribute color programmatically 
Java :: how to get multiple integer input in java 
Java :: cosinus-1 java 
Java :: android localdatetime 
Java :: how to read file from assets folder in android 
Java :: write a name and convert it to an array 
Java :: vscode java.home path 
Java :: swing setbounds 
Java :: java image draw 
Java :: object to int java 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =