☝🏽 Count the Number of Vowels in a String
This program counts how many vowels (a, e, i, o, u) are present in a given string. It first converts the string to lowercase, then iterates through each character and checks if it’s a vowel.
Input: Google
Expected Output: 3
public class VowelCount {
public static void main(String[] args) {
String s = "Hello";
int count = 0;
s = s.toLowerCase();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
System.out.println("Vowel count: " + count);
}
}Vowel count: 2⚖️ Check if a String is a Palindrome
This program checks if a given string reads the same forward and backward. It reverses the string and compares it with the original.
Input: madam
Expected Output: Palindrome
public class PalindromeCheck {
public static void main(String[] args) {
String s = "madam";
String reversed = "";
for (int i = s.length() - 1; i >= 0; i--) {
reversed += s.charAt(i);
}
if (s.equals(reversed)) {
System.out.println("Palindrome");
} else {
System.out.println("Not a Palindrome");
}
}
}Palindrome🔁 Reverse a String
This program reverses the characters of a given string manually using a loop.
Input: Java
Expected Output: avaJ
public class ReverseString {
public static void main(String[] args) {
String s = "Java";
String reversed = "";
for (int i = s.length() - 1; i >= 0; i--) {
reversed += s.charAt(i);
}
System.out.println("Reversed String: " + reversed);
}
}Reversed String: avaJ🔤 Count the Number of Words in a Sentence
This program splits a sentence based on whitespace and counts the number of words.
Input: Java is awesome
Expected Output: 3
public class WordCount {
public static void main(String[] args) {
String s = "Java is awesome";
String[] words = s.trim().split("\\\\s+");
System.out.println("Word count: " + words.length);
}
}Word count: 3✔️ Check if Two Strings Are Anagrams
This program checks whether two strings are anagrams by sorting their characters and comparing them.
Input: listen, silent
Expected Output: Anagrams
import java.util.Arrays;
public class AnagramCheck {
public static void main(String[] args) {
String s1 = "listen";
String s2 = "silent";
char[] a1 = s1.toCharArray();
char[] a2 = s2.toCharArray();
Arrays.sort(a1);
Arrays.sort(a2);
if (Arrays.equals(a1, a2)) {
System.out.println("Anagrams");
} else {
System.out.println("Not Anagrams");
}
}
}Anagrams␣ Remove All Whitespaces from a String
This program removes all whitespace characters (spaces, tabs, newlines) from a string using regex.
Input: " Java Programming "
Expected Output: JavaProgramming
public class RemoveSpaces {
public static void main(String[] args) {
String s = " Java Programming ";
s = s.replaceAll("\\\\s+", "");
System.out.println("Without spaces: " + s);
}
}Without spaces: JavaProgramming🧮 Find the Frequency of a Character in a String
This program counts how many times a specific character appears in a given string.
Input: programming, character: g
Expected Output: 2
public class CharFrequency {
public static void main(String[] args) {
String s = "programming";
char target = 'g';
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == target) {
count++;
}
}
System.out.println("Frequency of '" + target + "': " + count);
}
} Frequency of 'g': 2