desislava_topuzakova

01. Reversed String

Mar 27th, 2022
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package TextProcessing;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ReverseStrings_01 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String word = scanner.nextLine(); //"end" или на друг текст
  9.  
  10.         //повтарям: въвеждам нова дума
  11.         //спирам: дума == "end"
  12.         //продължавам: дума != "end"
  13.         while (!word.equals("end")) {
  14.             //дадената дума -> "Softuni"
  15.             //обратен ред -> "inutfoS"
  16.             String reversedWord = getReversedWord(word);
  17.  
  18.             //"{word} = {reversed word}"
  19.             System.out.println(word + " = " + reversedWord);
  20.  
  21.             word = scanner.nextLine();
  22.         }
  23.     }
  24.  
  25.     private static String getReversedWord(String word) {
  26.         //table -> elbat
  27.         String reversedWord = "";
  28.         for (int position = word.length() - 1; position >= 0 ; position--) {
  29.             char currentSymbol = word.charAt(position);
  30.             reversedWord += currentSymbol;
  31.         }
  32.         return reversedWord;
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment