using strcmp

pllllllllllzzzzz someone help me out for this plzzzzz...i want to output equal words from words groups n1 and n1. this program just giving value of the equal words.not the words.plzzz help meeeeeee


// my.lab.5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
char words1[15][20];
char words2[15][20];

int n1;
int n2;
int r1;

cout<< "how many words you wish to enter:";
cin>> n1;
cout<< "please enter 1st line:";
for(int i=0; i<n1; i++)
{
cin>> words1[i];

}

cout<< "how many words you wish to enter:";
cin>> n2;
cout<< "please enter 2nd line:";
for(int j=0; j<n2; j++)
{
cin>> words2[j];
}

r1=0;
for(int m=0; m<n1; m++)
{
//calculate and repeat, word1[m] in word2[]
for( int k=0; k<n2; k++)
{
if(!strcmp(words1[m],words2[k]))
{
r1++;
}
}
}
cout<< "number of equal words:" << r1 ;


cin.get();
cin.get();
cin.get();


return 0;
}
closed account (D80DSL3A)
Your opportunity to print the matching words is right here:
1
2
3
4
if(!strcmp(words1[m],words2[k]))
{
r1++;
}

Just insert the code to do so. You could even print the word positions in the lines since those are known as well.
sorry i didnt get it!.i have already write these line???:( i am very new one for programming. so plz if u can rewrite program with codes. plz
closed account (D80DSL3A)
I meant that the code for printing each word that appears in both lists should go within the code I pulled from your program:
1
2
3
4
5
if(!strcmp(words1[m],words2[k]))
{
// add a line here to print out the word (either words1[m] or words2[k], they are the same)
r1++;
}

I hope that clarifies things enough.
ok i got it. but the problem how i can print that word?like u said i change program see below. but i want to prind that equal words:(



#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
char words1[15][20];
char words2[15][20];

int n1;
int n2;
int r1;

cout<< "how many words you wish to enter:";
cin>> n1;
cout<< "please enter 1st line:";
for(int i=0; i<n1; i++)
{
cin>> words1[i];

}

cout<< "how many words you wish to enter:";
cin>> n2;
cout<< "please enter 2nd line:";
for(int j=0; j<n2; j++)
{
cin>> words2[j];
}

r1=0;
for(int m=0; m<n1; m++)
{

for( int k=0; k<n2; k++)
{
if(strcmp(words1[m],words2[k]) == 0)
{
words1[m];
r1++;
}
}
}
cout<< "number of equal words:" << r1 ;


cin.get();
cin.get();
cin.get();


return 0;
}
for a example if i enter "cat dog " for word1[] and "rat cat" for word2[] i want to get 'cat' as the output.plz help me out
ooooh hope i did it her below:))) thanks a lot fun2code for helping me out !!!!!!!! thanks again!!!!


r1=0;
for(int m=0; m<n1; m++)
{

for( int k=0; k<n2; k++)
{
if(strcmp(words1[m],words2[k]) == 0)
{
words1[m];
r1++;
cout<< "here are the aqual words:" << words1[m] << '\n';
}
}
}
cout<< "number of equal words:" << r1
closed account (D80DSL3A)
You're welcome.
Topic archived. No new replies allowed.