Reputation: 15
I would like to compare two user-defined strings and output the count of the number of characters shared between the two strings, without resorting to using arrays. I then need to output each of those characters. I understand the user-input part using a Scanner, but afterwards I am clueless.
For example, "hamper" as string1, and "happened" as string2 would return:
number of shared characters = 5
shared characters >> "h", "a", "p", "p", "e", "e"
Here is what i have so far. It prints each character on a separate line though. Is there a way without arrays to list them all on one line like above?:
public class CountMatches {
public static void main(String[] args)
{
//Declare both Strings.
String word1;
String word2;
int count = 0;
//Call for User Input.
Scanner inputDevice = new Scanner(System.in);
System.out.print("Input String 1 >> ");
word1 = inputDevice.next();
System.out.print("Input String 2 >> ");
word2 = inputDevice.next();
inputDevice.close();
//Determine lengths and set label accordingly.
String BigWord;
String SmallWord;
if (word1.length() > word2.length())
{
BigWord = word1;
SmallWord = word2;
}
else
{
BigWord = word2;
SmallWord = word1;
}
//Count and Display the like characters.
for (int i = 0; i < SmallWord.length(); i++)
{
if (BigWord.contains(String.valueOf(SmallWord.charAt(i))))
{
System.out.println("both words contain the letter " + SmallWord.charAt(i));
count++;
}
}
//Display the count of like characters.
System.out.print("Number of like characters >> " + count);
}
}
Upvotes: 1
Views: 1752
Reputation: 30320
Let's say you have word1
and word2
:
String biggerWord;
String smallerWord;
if (word1.length() > word2.length()) {
biggerWord = word1;
smallerWord = word2;
} else {
biggerWord = word2;
smallerWord = word1;
}
for (int i = 0; i < smallerWord.length(); i++) {
if (biggerWord.contains(String.valueOf(smallerWord.charAt(i)))) {
counter++;
}
}
This figures out which word is bigger. Then for the length of smallerWord
, iterate over it one character at a time and see if biggerWord
contains that character. If it does, increment the counter.
counter
should then have the number of common characters by the end of the loop.
This was written freehand, so be aware of syntax and minor logic errors. Or I misunderstood your assignment. It should be pretty close though.
Upvotes: 1
Reputation: 258
A really nice way is to sort the string alphabetically.
sortedWord1 = new String(Arrays.sort(word1.toCharArray()));
sortedWord2 = new String(Arrays.sort(word2.toCharArray()));
What that does is turn the words into character arrays, sort them alphabetically, then makes them into a string again.
The next step is just to iterate from the beginning and print out all common characters. This would be easier since they're sorted.
int index1 = 0;
int index2 = 0;
while((index1 < sortedWord1.length()) && (index2 < sortedWord2.length()) {
if(sortedWord1.charAt(index1) == sortedWord2.charAt(index2)) {
System.out.print(sortedWord1.charAt(index1) + " ");
index1++; index2++;
}
else if(sortedWord1.charAt(index1)> sortedWord2.charAt(index2)) {
index2++;
}
else {
index1++;
}
}
I haven't checked it for syntax errors, but it should be good.
Upvotes: 0