LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   separating words (https://www.linuxquestions.org/questions/programming-9/separating-words-56973/)

cxel91a 04-27-2003 04:17 AM

separating words
 
I'm working on this project to find the number count for a specific word in a file.
I need to put each word into an array, but I'm having problems setting it up.
Can someone help me. This is my own personal project, NOT school homework.

//Program to count words in a file

#include<iostream>
using namespace std;
#include<fstream>
#include<cstdlib>
#include<cstring>


int main(int argc, char* argv[])
{
if ( argc <= 2)
{
cout<<"Usage: program filename arg1 \n";
exit(1);
}

ifstream inFile;
if(!inFile)
{
cout<<"Error cannot create inFile object\n";
exit(1);
}

inFile.open(argv[1]);
if(inFile.fail())
{
cout<<"Error cannot open file\n";
exit(1);
}

inFile.seekg(0L,ios::beg);

char buffer[80];
while ( inFile.peek() != EOF)
{
inFile.getline(buffer,80);
cout<<buffer<<endl;

// Problems here <-

}


return 0;
}

david_ross 04-27-2003 09:58 AM

Why not use perl. You could do the count in a line.

Tinkster 04-27-2003 06:59 PM

How do you like this aproach? :)

Not quite using an array, but very
cplusplussy ;)

Code:

#include <iostream>
#include <fstream>
#include <string>
#include <map>

using namespace std;

map<string, int> count;
string w;

int main(int argc, char* argv[])
{
  cout << endl << argc << endl;
  if ( argc < 2)
    {
      cout<<"Usage: program filename arg1 \n";
      exit(1);
    }

  ifstream inFile;
  if(!inFile)
    {
      cout<<"Error cannot create inFile object\n";
      exit(1);
    }

  inFile.open(argv[1]);
  if(inFile.fail())
    {
      cout<<"Error cannot open file\n";
      exit(1);
    }

  inFile.seekg(0L,ios::beg);

  while ( inFile >> w )
    {
    ++count[w];
    }

  for (map<string, int>::const_iterator it=count.begin();
      it != count.end() ; ++it)
    {
      cout << it->first << "\t" << it->second << endl;
    }

  return 0;
}


Cheers,
Tink

Tinkster 04-27-2003 08:34 PM

Quote:

Originally posted by david_ross
Why not use perl. You could do the count in a line.
More elegant than that? :)
Code:

  while ( inFile >> w )
    {
    ++count[w];
    }

STL owns you ;)


Cheers,
Tink

cxel91a 04-28-2003 01:44 AM

I tried to compile your code but it will not work, but I'm looking into the STL you provided.

Tinkster 04-28-2003 01:53 AM

Compiles here ... and without using STLPort

What compiler are you using, what
error message do you get?

Cheers,
Tink

cxel91a 04-28-2003 03:33 AM

I'm using
c++ (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
I also changed the header file for map to map.h

These are the errors with #include <map.h>

In file included from /usr/include/c++/3.2/backward/map.h:59,
from countwords.cpp:4:
/usr/include/c++/3.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
countwords.cpp: In function `int main(int, char**)':
countwords.cpp:38: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std::pair<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here
countwords.cpp:41: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std::pair<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here
countwords.cpp:42: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std::pair<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here


These errors with #include <map>

countwords.cpp: In function `int main(int, char**)':
countwords.cpp:38: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std::pair<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here
countwords.cpp:41: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std::pair<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here
countwords.cpp:42: use of `count' is ambiguous
countwords.cpp:8: first declared as `std::map<std::string, int,
std::less<std::string>, std::allocator<std::pair<const std::string, int> > >
count' here
/usr/include/c++/3.2/bits/stl_algo.h:390: also declared as `
std::iterator_traits<_Iterator>::difference_type std::count(_InputIter,
_InputIter, const _Tp&)' here

Tinkster 04-28-2003 02:28 PM

gcc 2.95.3 here :}

Guess I'll have to re-check my syntax if 3.2 is
nagging about it :D

Cheers,
Tink

david_ross 04-28-2003 02:49 PM

Quote:

Originally posted by Tinkster
More elegant than that? :)
Code:

  while ( inFile >> w )
    {
    ++count[w];
    }

STL owns you ;)


Cheers,
Tink

Not bad - I still prefer something in perl:
Code:

#!/usr/bin/perl

while(<STDIN>){$count += $_ =~ s/$ARGV[0]//g}
print "There are $count occurences of $ARGV[0]\n";

exit;

Call it using "cat myfile | wc.pl STRING". (Assuming wc.pl is in your path, your file is called "myfile" and you want to search for STRING)

Tinkster 04-28-2003 03:27 PM

I want to know how often each word was in myfile :)

Code:


  //the most amazing thing is that this small loop
  //does the entire counting/splitting up ;)
  while ( inFile >> w )
    {
    ++count[w];
    }

  //that one just does the output :)
  //of the words & counts
  for (map<string, int>::const_iterator it=count.begin();
      it != count.end() ; ++it)
    {
      cout << it->first << "\t" << it->second << endl;
    }

Cheers,
Tink

david_ross 04-28-2003 03:55 PM

Quote:

Originally posted by Tinkster
I want to know how often each word was in myfile :)
Then use this:

Code:

#!/usr/bin/perl

# Read the data from STDIN
while(<STDIN>){$string .= $_}

# Run through each word and increment an associative array based on the word
foreach $word (split(/\s|\.|\?|\!|\"|\'|\(|\)|\:|\;/,$string)){if($word){$count{$word}++}}

# Sort the array then print the word count
foreach $key (sort keys %count){print "$key = $count{$key}\n"}

exit;

Only one more line than before!

Anything else? :D

Tinkster 04-28-2003 05:31 PM

/me shudders:"Take it away! It hurts us!!" :D

Cheers,
Tink

Satriani 04-29-2003 10:01 AM

cat /path/filename |grep -i lookupword |wc -l

Can it be any more easy??

Satriani 04-29-2003 10:03 AM

Oops, sorry, should read better, missed the array part.....

david_ross 04-29-2003 01:05 PM

It also doesn't take punctuation into account eg

"hello" != "hello."
"blah!" != "blah"


All times are GMT -5. The time now is 04:54 AM.