string programs in java

ww‮ruttual.w‬i.com
string programs in java

Here are a few examples of simple string programs in Java:

  1. Reverse a string:
public static String reverse(String s) {
    StringBuilder sb = new StringBuilder(s);
    return sb.reverse().toString();
}
  1. Check if a string is a palindrome (a word or phrase that reads the same backward as forward):
public static boolean isPalindrome(String s) {
    String reversed = reverse(s);
    return s.equalsIgnoreCase(reversed);
}
  1. Count the number of vowels in a string:
public static int countVowels(String s) {
    int count = 0;
    for (char c : s.toCharArray()) {
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
            count++;
        }
    }
    return count;
}
  1. Remove all spaces from a string:
public static String removeSpaces(String s) {
    return s.replaceAll("\\s", "");
}

These are just a few examples, but there are many other string-related programs you can write in Java. You can find more information and examples in the Java documentation and online resources.

Created Time:2017-10-17 20:18:55  Author:lautturi