there two article:A ,B,which is very large. get three or more successive words in A,to find if it appears in B ,and count the times. For example , 'book' 'his' 'her' appear in A ,how many times it appears in B?
SOLUTION:
#include <iostream.h>
#include <cstdio>
#include <map.h>
#include <string.h>
using namespace std;
void findArr(string a, string out[])
{
string s="";
int j=0;
for(int i=0; i<=a.length(); i++)
{
if(a[i]!=' ' && a[i]!='\0')
{
s+=a[i];
}
else
{
out[j]=s+'\0';
j++;
s="";
}
}
}
int size( string a)
{
int count =0;
for(int i=0; i
{
if(a[i] == ' ')
count++;
}
return count+1;
}
int main()
{
string a="hello bye tea coffee";
string b="hello hello tea";
string arr1[size(a)];
string arr2[size(b)];
findArr(a,arr1);
findArr(b,arr2);
map<string,int> store;
for(int i=0; i<sizeof(arr1)/sizeof(string); i++)
{
store.insert(pair(arr1[i],0));
}
map<string,int> :: iterator it;
for(int i=0; i
{
it=store.find(arr2[i]);
if(it!=store.end())
{
store[arr2[i]]+=1;
}
}
for(int i=0; i<sizeof(arr1)/sizeof(string); i++)
{
cout<<"\nString "<<arr1[i] << " occurs in B " << store[arr1[i]] << " number of times"<< endl;
}
}

No comments:
Post a Comment