| Q & A Home |
| Math |
| Science |
| History |
| IT & Web |
| Programming |
| Health |
| Business |
| Arts & Humanities |
| Social Studies |
| Engineering & Technology |
| Arts & Entertainment |
| Humanities |
| Sports |
| Auto |
| Hobbies |
| Books and Literature |
| Electronics |
| Food & Drink |
| Jobs & Education |
| Law & Government |
| Travel & Places |
| People & Society |
| Beauty & Health |
| Animals & Plants |
| Other |
Ahmad Naima 29 Jun, 2024 11:24:30 PM 1 38
Best Answer: Two strings are anagrams if they contain a similar group of characters in a varied sequence.
Declare a boolean variable that tells at the end of the two strings are anagrams or not.
First, check if the length of both strings is the same, if not, they cannot be anagrams.
Convert both the strings to character arrays and then sort them.
Check if the sorted arrays are equal. If they are equal, print anagrams, otherwise not anagrams.
boolean anagrmstat = false;
if (str.length() != reverse.length()) {
System.out.println(str + " and " + reverse + " not anagrams string");
} else {
char[] anagram1 = str.toCharArray();
char[] anagram2 = reverse.toCharArray();
Arrays.sort(anagram1);
Arrays.sort(anagram2);
anagrmstat = Arrays.equals(anagram1, anagram2);
}
if (anagrmstat == true) {
System.out.println(" anagrams string");
} else {
System.out.println(" not anagrams string");
}
25. How do you calculate the number of vowels and consonants in a String?
Loop through the string.
Increase the vowel variable by one whenever the character is found to be a vowel, using the if condition. Otherwise, increment the consonant variable.
Print the values of both the vowel and the consonant count.
int vowels = 0;
int consonants = 0;
for (int k = 0; k < str.length(); k++) {
char c = str.charAt(k);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
vowels++;
else
consonants++;
}
System.out.println("Vowel count is " + vowels);
System.out.println("Consonant count is: " + consonants);
Ahmad Naima 29 Jun, 2024 11:24:30 PM