-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringAssignment.java
42 lines (36 loc) · 1.38 KB
/
StringAssignment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Arrays;
public class StringAssignment {
public static void main(String[] args) {
System.out.println("Lowercase vowels: "+lowercaseVowels("ABaCDeiEFoUbdsu"));
String str = "ApnaCollege".replace("l","");
System.out.println("Remove l: "+str);
System.out.println("Anagram: "+anagram("heart", "earth"));
}
public static int lowercaseVowels(String str){
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++;
}
}
return count;
}
// If two strings contain the same characters but in a different order, they can be said to be anagrams.
// Consider race and care.In this case, race's characters can be formed into a care, or care's characters can be formed into race.
public static boolean anagram(String str1, String str2){
if(str1.length()==str2.length()){
char[] str1array = str1.toCharArray();
char[] str2array = str2.toCharArray();
Arrays.sort(str1array);
Arrays.sort(str2array);
if(Arrays.equals(str1array, str2array)){
return true;
}else{
return false;
}
}else{
return false;
}
}
}