eliminate the character

closed account (iN8poG1T)
Hello, does anyone know how to eliminate the character? left only number and words


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>

using namespace std;


int main()
{
    // For convenience
  typedef std::map<std::string, int> FreqMap;

  std::string word; // the input word
  FreqMap wf;       // the frequencies of each word

    // Open some text file
  std::ifstream infile("2.txt");

    // Read all words from the file
  while (infile >> word)
  {
      // See if the key/value pair is already
      // in the map
    FreqMap::iterator it = wf.find(word);

      // If it is present, increment the count (value)
    if (it != wf.end())
      it->second++;  // Same as: (*it).second++
    else
    {
        // Create a new pair with value set to 1
      std::pair<std::string, int> pr(word, 1);
      wf.insert(pr);
    }
  }

    // Print out all of the key/value pairs
  for (FreqMap::iterator it = wf.begin(); it != wf.end(); ++it)
    std::cout << it->second << " " << it->first << std::endl;
}




Last edited on
Hello sjh,

What character?

The program appears to deal with words, but the question leads one to believe that you want to deal with a single character.

A good sample of or the input would help. Not knowing what you are using makes it hard to test the program.

Andy
closed account (iN8poG1T)
I have a text file


1.txt
"And that's because the population has increasingly become dominated
by older and larger animals. This is a result of a decline in the
number of krill entering the population - what we call juvenile
recruitment."


okay now, if i read the file it will be like this


" - no of occurence
And
This 
a
animals.
because 
become
by 
decline 
dominated
call 
entering 
the 
that's 
has 
increasingly
in 
is 
juvenile
krill 
larger 
result 
number 
older 
of 
population 
- 
what 
we 
recruitment.
"


as you can see, theres a character " ". and also in (that's) has some ' but now i want it to be removed. also i want to removed the word that's bcs there is some character in there
the output will be in ascending order alphabetically, the number of occurence will be next to the word, if there is double word, it will output only one and show the no of occurence

Last edited on
closed account (iN8poG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>

using namespace std;

int main()
{
    const string path = "2.txt";
    ifstream input( path );

    if ( !input )
    {
        cerr << "Error opening file.\n";
        return 0;
    }

    multimap< string, int >  words;

    char buf[ 255 ];
    for ( int line = 1; input.getline( buf, sizeof buf ); line++ )
    {
        for ( char *p = buf; *p; ++p )
            if ( !isalpha( *p ) )
                *p = ' ';

        string word;
        for ( istringstream iss( buf ); iss >> word; )
            words.insert( make_pair( word, line ) );
    }

    for ( auto it1 = words.begin(); it1 != words.end(); )
    {
        auto it2 = words.upper_bound( it1->first );
        cout << it1->first << " : ";
        for ( ; it1 != it2; it1++ )
            cout << it1->second << ' ';
        cout << '\n';
    }
}



I tried this code but this will eliminate all character and number which i only want to eliminate character not number, also, it shows which the line for each word which i dont want, i want it only to count the word exist not showing the line in where the word exist

side note : the text file is too long so i only post little bit of it

Thank you
Hello sjh,

I tried this bit of code:
1
2
3
4
5
6
7
8
9
10
while (infile >> word)
{
	if (word == "-") continue;
	word.erase(std::remove(word.begin(), word.end(), '\"'), word.end());  // <--- Removes the quotes.
	word.erase(std::remove(word.begin(), word.end(), '.'), word.end());  // <--- Removes the period.
	word.erase(std::remove(word.begin(), word.end(), '\''), word.end());  // <--- Removes the apostrophe.

	// See if the key/value pair is already
	// in the map
	FreqMap::iterator it = wf.find(word);

After that the rest of the program works.

Someone more familiar with "regex" may be able to do all of this with one or two lines of code.

Hope that helps,

Andy
closed account (iN8poG1T)
Hai Andy,

i already tried that but the problem is i get an error

error: cannot convert 'std::__cxx11::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >}' to 'const char*' for argument '1' to 'int remove(const char*)'
Last edited on
Hello sjh,

Sorry in my haste I forgot to mention to include the "algorithm" header file.

A thought I have is that the line of code may not work under the C++11 standards because it was not available until the C++14 standards and the reason it worked for me is that My IDE and compiler are set for the C++14 standards.

It may not be as neat and compact, but I will see what I can come up with that will work in the C++11 standards.

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cctype>
#include <map>
using namespace std;

string filter( string input )
{
   string output;
   for ( char c : input ) if ( isalnum( c ) ) output += tolower( c );
   return output;
}


int main()
{
   map<string,int> freq;
// ifstream in( "input.txt" );
   stringstream in( "We hold these truths to be self-evident, that all men are created equal, "
                    "that they are endowed by their Creator with certain unalienable Rights, "
                    "that among these are Life, Liberty and the pursuit of Happiness." );

   for ( string word; in >> word; )
   {
      word = filter( word );
      if ( word.size() ) freq[word]++;
   }
   
   for ( auto p : freq ) cout << p.first << "  " << p.second << '\n';
}


all  1
among  1
and  1
are  3
be  1
by  1
certain  1
created  1
creator  1
endowed  1
equal  1
happiness  1
hold  1
liberty  1
life  1
men  1
of  1
pursuit  1
rights  1
selfevident  1
that  3
the  1
their  1
these  2
they  1
to  1
truths  1
unalienable  1
we  1
with  1
bool remover(char c)
{ return (c=='.' || c=='\'' || c == '\"'); } //could be lambda but my compiler does not have
s.erase(std::remove_if(s.begin(), s.end(),remover),s.end());

but I think the filter version is what was really wanted.
Last edited on
Topic archived. No new replies allowed.