Click here to Skip to main content
15,881,804 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Any idea how to write a program using function and string where the user inputs the text and output each word in the text and how many times it is repeated beside it.

Example:
cout<< " Enter the text";

cin>> Hello this is a test 1. Hello this is a test 2.;

cout<<
hello 2
this 2
is 2
a 2
test 2
1 1
2 1

-------
Thankx in advance
Posted
Comments
Richard MacCutchan 16-Oct-11 3:41am    
Since it is your homework you are expected to work it out for yourself.

 
Share this answer
 
v2
I did not realize this was a homework forum when I joined. jeez.
 
Share this answer
 
Comments
Richard MacCutchan 16-Oct-11 3:40am    
It isn't!
Solution 1 looks neat and precise...
Alternatively you may want to use the code block below which basically splits your input into individual words and then you can add your code for doing the count

C++
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main (int argc, char * const argv[]) {
    
    cout << "Enter the text\n";
	string input;
	getline(cin,input);
	
	char *delim = " ,.-";              // This will break the input string for following characters { } {,} {.} {-}. You can add more like semi-colon(;), colon(:), etc. as per your input conditions.
	
	char* split = strtok((char *)input.c_str() , delim);
	
	vector<string> result;
	
	while (split != NULL) {
		result.push_back(split);
		split = strtok(NULL, delim);
	}
	
	// Now iterate through the vector to get the count. Make sure you delete the repeated occurance once you have counted them.
	
	return 0;
}
 
Share this answer
 
#include <stdio>


void main()

{
char arr[44];
cout<<"Enter any string\t";
gets(arr);
getch();}

//////////wesit my email address ********* REMOVED ******
 
Share this answer
 
v2
Comments
Albert Holguin 16-Oct-11 13:49pm    
Unless you really love spam, don't post your email address...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900