Java: How To Count Words

public class Main {
  public static void main(String[] args) {
    String words = "One Two Three Four";
    int countWords = words.split("\\s").length;
    System.out.println(countWords);
  }
}

// Outputs
4

Reverse a String

public class Main {
  public static void main(String[] args) {
    String originalStr = "Hello";
    String reversedStr = "";
    System.out.println("Original string: " + originalStr);
        
    for (int i = 0; i < originalStr.length(); i++) {
      reversedStr = originalStr.charAt(i) + reversedStr;
    }
    
    System.out.println("Reversed string: "+ reversedStr);
  }
}

 

posted @ 2022-11-27 23:45  小白冲冲  阅读(47)  评论(0)    收藏  举报