[ create a new paste ] login | about

Link: http://codepad.org/xvM1MuBy    [ raw code | output | fork ]

C++, pasted on Jan 16:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main()
{
  map<string,int> dict;
  map<string,int>::iterator it; 
  char str[] = "This is a pen. That is a pencil. He is tall."; 
  char *p;
  p = strtok(str, " .,");
  while(p){
       if((it=dict.find(p))!=dict.end()) it->second++;
       else dict.insert(pair<string, int>(p, 1));

       p = strtok(0, " .,");
  }
  //result
  for(it=dict.begin(); it!=dict.end(); it++)
      cout << it->first << " : " << it->second << endl;
}


Output:
1
2
3
4
5
6
7
8
He : 1
That : 1
This : 1
a : 2
is : 3
pen : 1
pencil : 1
tall : 1


Create a new paste based on this one


Comments: