Skip to main content

Coding Interview : How do you reverse word of a sentence in Java?

Problem statement says that we need to reverse the each word of a given sentence.

Program : 
package net.jubiliation.example;
/**
 * @author Gaurav
 *
 */
public class StringReverseExample {

 public static void reverseWordInMyString(String sentence) {

  String[] words = sentence.split(" ");
  /*
   * Here split() method of String class splits a string in chunks based
   * on the delimiter passed as an argument to it.
   */
  String reversedSentence = "";

  for (int index = 0; index < words.length; index++) {
   String word = words[index];
   String reverseWord = "";

   for (int temp = word.length() - 1; temp >= 0; temp--) {
    /*
     * The charAt() function returns the character at the given position in a string.
     */
    reverseWord = reverseWord + word.charAt(temp);
   }
   reversedSentence = reversedSentence + reverseWord + " ";
  }

  System.out.println(sentence);
  System.out.println(reversedSentence);

 }

 public static void main(String[] args) {

  reverseWordInMyString("Welcome to jubiliation");
  reverseWordInMyString("Example to reverse the words of sentence");

 }
}
Result :
Welcome to jubiliation
emocleW ot noitailibuj
Example to reverse the words of sentence
elpmaxE ot esrever eht sdrow fo ecnetnes 

Comments

Popular posts from this blog

Who is Peter Lynch and what is his philosophy in equity market investment? 25 Golden Rules of the most successful Fund Manager.

Peter Lynch (born January 19, 1944) is an American investor, mutual fund manager, and philanthropist. As the manager of the Magellan Fund at Fidelity Investments between 1977 and 1990, Lynch averaged a 29.2% annual return, consistently more than doubling the S&P 500 stock market index and making it the best-performing mutual fund in the world. During his 13 year tenure, assets under management increased from $18 million to $14 billion. He also co-authored a number of books and papers on investing and coined a number of well known mantras of modern individual investing strategies, such as Invest in what you know and ten bagger. Lynch is consistently described as a "legend" by the financial media for his performance record. Base on his career I have compiled his investing rules here. 25 GOLDEN RULES by @Peter Lynch 1: Nobody can predict interest rates, the future direction of the economy or the stock market. Dismiss all such forecasts & concentrate on what's actual

What is version checking in Hibernate ?

Version checking used in hibernate when more then one thread trying to access same data. For example : User A edit the row of the TABLE for update ( In the User Interface changing data This is user thinking time) and in the same time User B edit the same record for update and click the update. Then User A click the Update and update done. Change made by user B is gone. In hibernate you can prevent slate object updation using version checking. Check the version of the row when you are updating the row. Get the version of the row when you are fetching the row of the TABLE for update. On the time of updation just fetch the version number and match with your version number (on the time of fetching).

What are JEE Containers? What are their different types?

Containers are the interface between a component and the low-level, platform-specific functionality that supports the component. Before it can be executed, a web, enterprise bean, or application client component must be assembled into a Java EE module and deployed into its container. The assembly process involves specifying container settings for each component in the Java EE application and for the Java EE application itself. Container settings customize the underlying support provided by the Java EE server, including such services as security, transaction management, Java Naming and Directory Interface (JNDI) API lookups, and remote connectivity. Here are some of the highlights. The Java EE security model lets you configure a web component or enterprise bean so that system resources are accessed only by authorized users. The Java EE transaction model lets you specify relationships among methods that make up a single transaction so that all methods in one transaction are treated as a