Hackerss.com

hackerss
hackerss

Posted on

Invertir un string in Java

Invertir un string - Java:


public class InvertirString {
   /**
   * This method invert a string.
   * @param str String to invert.
   * @return String Obtains the inverted string.
   */   
   public String invertirString(String str) {
       //Variable to store the inverted string
       String inverted = "";
       //Invert the string
       for (int i = str.length()-1; i >= 0; i--) {
           inverted += str.charAt(i);
       }
       //Return the inverted string
       return inverted;
   }

   /**
   * One Example
   */
   public static void main(String args[]) throws IOException {

      InvertirString  invertirString= new InvertirString ();
      //Example
      String invertido = invertirString.invertirString("Hola Mundo");
      //Output
      System.out.println("Inverted String is :" + invertido); //"odnuM olH"
   }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)