473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

counting input words

any comments for improvement:

/* C++ Primer - 4/e
*
* CHAPTER 10 - Associative Containers
*
* EXERCISE - 10.7
* Write a program to count and print the number of times each word
* occured in the input.
*
*/
#include <iostream>
#include <string>
#include <vector>
#include <map>

int main()
{
std::map<std::string, intword_count;
std::string aword;
while( std::cin >aword )
{
++word_count[aword];
}
std::cout << "\n----------------------------------------------------\n";
for( std::map<std::string, int>::const_iterator iter =
word_count.begin();
iter != word_count.end(); ++iter )
{
std::cout << iter->first << " occured "
<< iter->second << " times "
<< std::endl;
}

return 0;
}

================= OUTPUT =======================

/home/arnuld/programs $ ./a.out
bazarov
Jack
Bux
Bux
Bazaro
Jack
Bux

----------------------------------------------------
Bazaro occured 1 times
Bux occured 3 times
Jack occured 2 times
bazarov occured 1 times
/home/arnuld/programs $

--
http://lispmachine.wordpress.com/
Nov 23 '07 #1
0 1201

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: regrat | last post by:
It's possible create a javascript which counts all words in a webpage until the point in which I click with the mouse. Any suggests?
8
by: DrNoose | last post by:
Hi! I'm writing a program that is supposed to read a line of input, count the words and the number of occurrences of each letter. Then it should prints the number of occurrences of each letter...
6
by: gk245 | last post by:
Basically, i want to make a function that will receive a sentence or phrase, and count its words. It would start like this (i think): #include <stdio.h> int count ( char sentence ) {...
3
by: Nhd | last post by:
I have a question which involves reading from cin and counting the number of words read until the end of file(eof). The question is as follows: Words are delimited by white spaces (blanks,...
4
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
2
by: gdarian216 | last post by:
I am writting a code that opens a file and gets the file name from a command line argument. I then open an output stream to print results of functions in a separate file. I have written a function...
1
by: powerej | last post by:
I have gotten the part of counting how many words are in the string, but the vowels just seem alien to me. Ive tried so many things but never close to a correct answer. I know I need to use...
4
by: sandvet03 | last post by:
Im a newby to programing 2 months exp.. I am trying to write code in C to take a input string from the user and then the input substring. I wish to compare the substring to the string and count the...
2
by: Geneses | last post by:
Hi! I'm using Eclipse as an IDE, I need to code a program where users enter a string and it computes the count for each vowel, and how many words are in the string. Users can do as many strings as...
6
by: HypeBeast McStreetwear | last post by:
Hi I'm supposed to be writing a program that counts in a given text, the words that contain at least three different vowels (a,e,i,o,u) For my test run I have to use "unquestionably", but I can't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.