Suppose str1 = "Apple"
and str2 = "pen"
. Now compare str1
and str2
and find out common letters. Then print str1
with the common letters uppercase. How to do that?
Output:
"aPPlE"
CodePudding user response:
str1 = "Apple"
str2 = "pen"
# make sure not other letters are in uppercase
str1 = str1.lower()
str2 = str2.lower()
for c in str1:
if c in str2:
str1 = str1.replace(c, c.upper())
print(str1)
Output > aPPlE
CodePudding user response:
String s1 = "Apple";
String s2 = "pen";
HashSet<Character> set = new HashSet();
for(char c: s1.toCharArray()) {
if(set.contains(c)) {
continue;
} else if(s2.indexOf(c) >=0 ) {
set.add(c);
}
}
StringBuilder sb = new StringBuilder();
for(char c: s1.toCharArray()) {
if(set.contains(c)) {
sb.append(Character.toUpperCase(c));
} else {
sb.append(c);
}
}
System.out.println(sb.toString());