String Handling
Solution:
1. Remove vowels from a string
public class RemoveVowels {
public static void main(String[] args) {
String str = "COMPUTER APPLICATIONS";
String result = str.replaceAll("[AEIOUaeiou]", "");
System.out.println(result);
}}
2. Longest word and its length
public class LongestWord {
public static void main(String[] args) {
String str = "TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN BAGAN";
String[] words = str.split(" ");
String longest = "";
for(String word : words) {
if(word.length() > longest.length()) longest = word;
}
System.out.println("The longest word: " + longest);
System.out.println("The length of the word: " + longest.length());
}}
3. Replace vowels with '*'
public class ReplaceVowels {
public static void main(String[] args) {
String str = "TATA STEEL IS IN JAMSHEDPUR";
System.out.println(str.replaceAll("[AEIOUaeiou]", "*"));
}}
4. First letters from words to form a string
public class FirstLetters {
public static void main(String[] args) {
String sentence = "Vital Information Resource Under Seize";
String[] words = sentence.split(" ");
StringBuilder sb = new StringBuilder();
for(String word : words) {
sb.append(word.charAt(0));
}
System.out.println(sb);
}}
5. Display palindrome words
public class PalindromeWords {
public static void main(String[] args) {
String sentence = "MOM AND DAD ARE NOT AT HOME";
String[] words = sentence.split(" ");
for(String word : words) {
String rev = new StringBuilder(word).reverse().toString();
if(word.equals(rev)) {
System.out.println(word);
} }}}
6. Reverse sentence word order
public class ReverseWords {
public static void main(String[] args) {
String sentence = "Computer is Fun";
String[] words = sentence.split(" ");
for(int i = words.length - 1; i >= 0; i--) {
System.out.print(words[i] + " ");
}}}
7. Word with max vowels
public class MaxVowels {
public static void main(String[] args) {
String str = "HAPPY NEW YEAR";
String[] words = str.split(" ");
String maxWord = "";
int maxCount = 0;
for(String word : words) {
int count = 0;
for(char ch : word.toCharArray()) {
if("AEIOUaeiou".indexOf(ch) != -1)count++;
}
if(count > maxCount) {
maxCount = count;
maxWord = word;
}
}
System.out.println("The word with maximum number of vowels: " + maxWord);
}}
8. Convert uppercase to lowercase and replace vowels
public class ReplaceVowelsNext {
public static void main(String[] args) {
String word = "computer";
word = word.toLowerCase();
StringBuilder sb = new StringBuilder();
for(char ch : word.toCharArray()) {
if("aeiou".indexOf(ch) != -1)
sb.append((char)(ch + 1));
else
sb.append(ch);
}
System.out.println(sb);
}}
9. Count words starting with 'A'
public class CountA {
public static void main(String[] args) {
String str = "advancement and application of information technology are ever
changing";
str = str.toUpperCase();
System.out.println(str);
String[] words = str.split(" ");
int count = 0;
for(String word : words) {
if(word.startsWith("A")) count++;
}
System.out.println("Total number of words starting with letter 'A' = " + count);
}}
10. Display each word in uppercase on new line
public class UpperCaseWords {
public static void main(String[] args) {
String str = "India is my country";
str = str.toUpperCase();
String[] words = str.split(" ");
for(String word : words) {
System.out.println(word);
}}}
11. Alternate characters from two strings
public class MergeStrings {
public static void main(String[] args) {
String s1 = "BALL";
String s2 = "WORD";
StringBuilder sb = new StringBuilder();
for(int i = 0; i < s1.length(); i++) {
sb.append(s1.charAt(i)).append(s2.charAt(i));
}
System.out.println(sb);
}}
12. Count double letter sequences
public class DoubleLetterCount {
public static void main(String[] args) {
String str = "SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE";
str = str.toUpperCase();
int count = 0;
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) == str.charAt(i + 1)) {
count++;
i++; // skip next character to avoid counting same pair twice
}
}
System.out.println("Double letter sequences: " + count);
}}
13. Palindrome or Special Word
public class SpecialPalindrome {
public static void main(String[] args) {
String word = "COMIC"; // Change to any input
word = word.toUpperCase();
boolean isPalindrome = word.equals(new StringBuilder(word).reverse().toString());
boolean isSpecial = word.charAt(0) == word.charAt(word.length() - 1);
if (isPalindrome) {
System.out.println("Palindrome");
} else if (isSpecial) {
System.out.println("Special Word");
} else {
System.out.println("None");
} }}
14. Consecutive letter pairs in words
public class ConsecutiveLetters {
public static void main(String[] args) {
String sentence = "MODEM IS AN ELECTRONIC DEVICE";
sentence = sentence.toUpperCase();
String[] words = sentence.split(" ");
int count = 0;
for (String word : words) {
boolean hasConsecutive = false;
for (int i = 0; i < word.length() - 1; i++) {
if (word.charAt(i) + 1 == word.charAt(i + 1)) {
hasConsecutive = true;
break;
}
}
if (hasConsecutive) {
System.out.println(word);
count++;
}
}
System.out.println("Number of words containing consecutive letters: " + count);
}}
15. Word pattern BLUEJ
public class BluejPattern {
public static void main(String[] args) {
String word = "BLUEJ";
// Pattern (a)
for (int i = word.length(); i > 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print(word.charAt(j) + " ");
}
System.out.println();
}
// Pattern (b)
for (int i = word.length() - 1; i >= 0; i--) {
for (int j = 0; j < word.length() - i; j++) {
System.out.print(word.charAt(i) + " ");
}
System.out.println();
}
// Pattern (c)
for (int i = 0; i < word.length(); i++) {
for (int j = i; j < word.length(); j++) {
System.out.print(word.charAt(j) + " ");
}
System.out.println();
}}}