I am a beginner at C++, and I am trying to create two strings
any suggestion?
I am a beginner at C++, and I am trying to create two strings
any suggestion?
Equals method will not help you in this situation. compare using charAt(). Use two for loop and iterate both strings then add not matching characters to one string and print it at last. ex:
for(int i=0;i<inputword.length;i++){
for(int j=0;i<inputword2.length;j++){
if(inputword.chatAt(i)==inputword2.charAt(j)){
//here write your logic or remove it from your string
}
}
}
To calculate how many characters at the start one word "overlap" the end of second:
public static int combinedLength(String s1, String s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
for (int i = 1; i < s1.length() && i < s2.length(); i++)
if (s1.endsWith(s2.substring(0, i+1)) || s2.endsWith(s1.substring(0, i+1)))
return s1.length() + s2.length() - i;
return s1.length() + s2.length();
}
This works by progressively testing longer letter sequences at the start/end if find if s1 starts with s2's end or visa versa. Because there can be only one such match, the first match found returns the result of the sum of both lengths minus the iteration number. No match returns the sum of both lengths.
Testing:
combinedLength("super", "perfect") ==> 9
combinedLength("perfect", "super") ==> 9
combinedLength("pencil", "eraser") ==> 12