0% found this document useful (0 votes)
10 views2 pages

_16.16_Strings Solutions

The document contains Java code solutions for string manipulation problems, including counting vowels in a string and checking if two strings are anagrams. Solution 1 counts the vowels in a user-input string, while Solution 4 checks if two predefined strings are anagrams by comparing their sorted character arrays. Additional outputs and explanations are provided for other solutions.

Uploaded by

Yashaswini B L
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)
10 views2 pages

_16.16_Strings Solutions

The document contains Java code solutions for string manipulation problems, including counting vowels in a string and checking if two strings are anagrams. Solution 1 counts the vowels in a user-input string, while Solution 4 checks if two predefined strings are anagrams by comparing their sorted character arrays. Additional outputs and explanations are provided for other solutions.

Uploaded by

Yashaswini B L
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/ 2

STRINGS SOLUTIONS

[email protected]
Solution 1:
import java.util.*;

public class Solution {


public static void main(String[] args) {
String str = new Scanner(System.in).next();
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("count of vowels is :" + count);
}
}

Solution 2: Output will be :


false true
(If you need an explanation, please r-ewatch the video about how Strings work in memory?)

Solution 3 : Output will be :


​ApnaCoege

Following are some methods in Java which are used to replace characters:
Solution 4:

[email protected]
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
String str1 = "earth";
String str2 = "heart";

//Convert Strings to lowercase. Why? so that we don't have to check


separately for lower & uppercase.
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();

// First check - if the lengths are the same


if(str1.length() == str2.length()) {
// convert strings into char array
char[] str1charArray = str1.toCharArray();
char[] str2charArray = str2.toCharArray();
// sort the char array
Arrays.sort(str1charArray);
Arrays.sort(str2charArray);
// if the sorted char arrays are same or identical then the strings are
anagram
boolean result = Arrays.equals(str1charArray, str2charArray);
if(result) {
System.out.println(str1 + " and " + str2 + " are anagrams of each
other.");
} else {
System.out.println(str1 + " and " + str2 + " are not anagrams of
each other.");
}
} else {
// case when lengths are not equal
System.out.println(str1 + " and " + str2 + " are not anagrams of each
other.");
}
}
}

You might also like