Write a program that will take two words entered by the user and determine how m
ID: 3878890 • Letter: W
Question
Write a program that will take two words entered by the user and determine how many letters they have in common. For example, suppose the user enters 'these and 'trees', then the program should output that the words have four letters in common (tese). Note that is letters are repeated, the smallest number of repeats is the number in common (tree and the' only have two letters in common). Hint: You may want to use a nested loop. Sample Program Run: Enter a word: these Enter another word: trees The words "these" and "trees" have 4 letters in common.Explanation / Answer
#include <iostream>
using namespace std;
int main() {
// your code goes here
string str1,str2;
cout<<"Enter a word ";
cin>>str1;
cout<<"Enter another word ";
cin>>str2;
int ar1[26],ar2[26];
for(int i=0;i<26;i++)
{
ar1[i]=0;
ar2[i]=0;
}
for(int i=0;i<str1.length();i++)
{
ar1[str1[i]-97]++;
}
for(int i=0;i<str2.length();i++)
{
ar2[str2[i]-97]++;
}
int sum=0;
for(int i=0;i<26;i++)
{
sum+= min(ar1[i],ar2[i]);
}
cout<<"The words "<<str1<<" and "<<str2<<" have "<<sum<<" words in common"<<" ";
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.