473,806 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::s tring, intword_count;
std::string aword;
while( std::cin >aword )
{
++word_count[aword];
}
std::cout << "\n----------------------------------------------------\n";
for( std::map<std::s tring, int>::const_ite rator iter =
word_count.begi n();
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 1222

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

Similar topics

8
2138
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
3862
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 that appears in the input line. For example, if I typed "I say Hi." it should give the following output: 3 words 1 a
6
1882
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 ) { char string ;
3
6357
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, tabs, linefeeds, returns). It will print 3 pieces of information before exit: the first word, the last word, and the number of words read, with one blank in between fields. Input lines of words Output
4
8444
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 needed for this assignment. 1.Counting Words in C++ Problem: Write an elegant C++ program to count the number of times each word occurs in a file. A word is defined as a sequence of characters surrounded by whitespace. Case is not important, so...
2
1454
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 and it isn't doing what I want it to. It is suppose to count the words in the file that was opened. the word_count function only incruments once giving a word count of one and I can figure out why. Can anyone help? this is my code. #include...
1
2432
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 charAt() and length() some nested loops with do while and for, but cant seem to get headed in the right direction. Also when i click the cancel button my program is suppose to end and tell you how many lines were entered, a count of vowels from all lines...
4
2047
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 number of times it apears and print this to the user. After many hours of frustration i have gotten this far any help or derection would be aprieciated # include<stdio.h> # include<math.h> # include<stdlib.h> # include<string.h> int...
2
4134
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 they want until they hit cancel. This is what I have so far: import javax.swing.*; public class Program { //Open Class public static void main(String args) { //Open Main String input, output; //Holds input and output. int aCount = 0,...
6
4291
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 anything to work. Here's what I have so far. #include <iostream> #include <string> #include <cstring> using namespace std;
0
9719
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10620
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10369
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10372
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7650
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.