GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Dec-2008, 12:26
princess princess is offline
New Member
 
Join Date: Dec 2008
Posts: 3
princess is on a distinguished road
Unhappy

Find common words between the two files


Hello, everyone!
Can anyone help me with this? 2 files are read and I must write a C++ program which has to show me all the common words between the two files. How can I do it? This is what I tried, but...

CPP / C++ / C Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{clrscr();
 char a[256], b[256], c[ ]=" ,.!?", *p, *q;;
 int found=0;
 cin.get(a,255);
 cin.get();
 cin.get(b,255);
 cin.get();
 p=strtok(a,c);
 while(p){q=strtok(b,c); while(q) {if(p==q) {found=1; cout<<p; p=(NULL,c);}q=(NULL,c);}}
 getch();}
Last edited by admin : 13-Dec-2008 at 12:55. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 13-Dec-2008, 12:27
princess princess is offline
New Member
 
Join Date: Dec 2008
Posts: 3
princess is on a distinguished road

Re: Please help me


I know that I can use strcmp( ), but I don't know how to write it.. Please help me! thank you
  #3  
Old 13-Dec-2008, 16:06
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Find common words between the two files


There is much outdated with your code (i.e. you should #include <iostream> instead of <iostream.h>, main return int, not void, etc). However, if this is to be C++ why not just use the std::string class? Couple that with a std::map (possibly, std::map < std::string, int >) and you can do something like (this is pseudocode)
CPP / C++ / C Code:
open file1
   for each word
      if word doesnt exist in map, assign it to 1
   end each word
close file1

open file2
   for each word
      if word exists in map then assign it to 2
   end each word
close file2

each item in the map
   if value is greater than 1  it is a match
end each

Note: Handling punctuation becomes more difficult, but the iostream/string classes have utilities for such things.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #4  
Old 14-Dec-2008, 05:12
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Find common words between the two files


Quote:
Originally Posted by L7Sqr
Note: Handling punctuation becomes more difficult

...particularly when dealing with words containing apostrophes, such as isn't, wouldn't, shouldn't, etc. versus 'single' 'quoted' words.

princess

The requirements lack detailed information as to what "common" means. For example, is "Name" and "name" common enough? If that's true, what about "Bob" a pronoun and "bob" a verb? What about when "bob" the verb is the first word in a sentence and is therefore capitalized? Is it the same word as Bob the pronoun?

Without a detailed definition of commonality, we can only assume that equality delineates commonality among words. Is that the objective of the application? Is that information specified in the requirements? If not, seek requirements elucidation before proceeding.

strtok is inherently unsafe. If you mean to use C and not C++, use it. If you mean to use C++, use std::string.


Bob the 'can
  #5  
Old 14-Dec-2008, 06:22
princess princess is offline
New Member
 
Join Date: Dec 2008
Posts: 3
princess is on a distinguished road

Re: Find common words between the two files


I didn't study any std::string class, std::map. It doesn't amtter if the world is a verb, a pronoun or whatever. OHH.....What can I do? How can I know the common words??? How to find them? give me a hit, something!
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:22.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.