0% found this document useful (0 votes)
9 views4 pages

Top 10 Java String Programs

The document provides a list of the top 10 Java string programs, showcasing both method-based and non-method-based implementations for each task. Examples include reversing a string, counting vowels, removing duplicates, and checking for palindromes or anagrams. Each program is accompanied by code snippets demonstrating the two approaches to solving the problem.

Uploaded by

Pradeep Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Top 10 Java String Programs

The document provides a list of the top 10 Java string programs, showcasing both method-based and non-method-based implementations for each task. Examples include reversing a string, counting vowels, removing duplicates, and checking for palindromes or anagrams. Each program is accompanied by code snippets demonstrating the two approaches to solving the problem.

Uploaded by

Pradeep Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Top 10 Java String Programs (With and Without Methods)

1. Reverse a string

With Method:
String str = "hello";
String reversed = new StringBuilder(str).reverse().toString();
System.out.println("Reversed: " + reversed);

Without Method:
String str = "hello";
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
System.out.println("Reversed: " + reversed);

2. Count vowels in a string

With Method:
String str = "education";
long count = str.chars().filter(ch -> "aeiou".indexOf(ch) >= 0).count();
System.out.println("Vowels: " + count);

Without Method:
String str = "education";
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
System.out.println("Vowels: " + count);

3. Remove duplicates from a string

With Method:
String str = "programming";
StringBuilder sb = new StringBuilder();
str.chars().distinct().forEach(c -> sb.append((char)c));
System.out.println(sb.toString());

Without Method:
String str = "programming";
String result = "";
for (int i = 0; i < str.length(); i++) {
if (result.indexOf(str.charAt(i)) == -1) {
result += str.charAt(i);
}
}
Top 10 Java String Programs (With and Without Methods)

System.out.println(result);

4. Find frequency of characters in a string

With Method:
String str = "success";
Map<Character, Long> freq = str.chars()
.mapToObj(c -> (char)c)
.collect(Collectors.groupingBy(c -> c, Collectors.counting()));
System.out.println(freq);

Without Method:
String str = "success";
int[] freq = new int[256];
for (int i = 0; i < str.length(); i++) {
freq[str.charAt(i)]++;
}
for (int i = 0; i < freq.length; i++) {
if (freq[i] > 0) {
System.out.println((char)i + ": " + freq[i]);
}
}

5. Count spaces in a string

With Method:
String str = "Java is simple";
long count = str.chars().filter(ch -> ch == ' ').count();
System.out.println("Spaces: " + count);

Without Method:
String str = "Java is simple";
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ') count++;
}
System.out.println("Spaces: " + count);

6. Count alphabets in a string

With Method:
String str = "Hello123";
long count = str.chars().filter(Character::isLetter).count();
System.out.println("Alphabets: " + count);

Without Method:
String str = "Hello123";
int count = 0;
for (int i = 0; i < str.length(); i++) {
Top 10 Java String Programs (With and Without Methods)

if ((str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') ||


(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')) {
count++;
}
}
System.out.println("Alphabets: " + count);

7. Check if string is palindrome

With Method:
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
System.out.println(str.equals(reversed) ? "Palindrome" : "Not Palindrome");

Without Method:
String str = "madam";
boolean isPalindrome = true;
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
isPalindrome = false;
break;
}
}
System.out.println(isPalindrome ? "Palindrome" : "Not Palindrome");

8. Check anagram strings

With Method:
String s1 = "listen", s2 = "silent";
char[] a = s1.toCharArray(), b = s2.toCharArray();
Arrays.sort(a); Arrays.sort(b);
System.out.println(Arrays.equals(a, b) ? "Anagram" : "Not Anagram");

Without Method:
String s1 = "listen", s2 = "silent";
if (s1.length() != s2.length()) {
System.out.println("Not Anagram");
} else {
int[] count = new int[256];
for (int i = 0; i < s1.length(); i++) {
count[s1.charAt(i)]++;
count[s2.charAt(i)]--;
}
boolean isAnagram = true;
for (int i : count) {
if (i != 0) isAnagram = false;
}
System.out.println(isAnagram ? "Anagram" : "Not Anagram");
}
Top 10 Java String Programs (With and Without Methods)

9. Convert String to Integer

With Method:
String str = "123";
int num = Integer.parseInt(str);
System.out.println("Integer: " + num);

Without Method:
String str = "123";
int num = 0;
for (int i = 0; i < str.length(); i++) {
num = num * 10 + (str.charAt(i) - '0');
}
System.out.println("Integer: " + num);

10. Split string into words

With Method:
String sentence = "Java is fun";
String[] words = sentence.split(" ");
for (String word : words) {
System.out.println(word);
}

Without Method:
String sentence = "Java is fun";
String word = "";
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) != ' ') {
word += sentence.charAt(i);
} else {
System.out.println(word);
word = "";
}
}
if (!word.isEmpty()) System.out.println(word);

You might also like