Click here to Skip to main content
15,064,072 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I need to write a program that take a string from user and print the repeated words in a string with the number of ocurence of repeated words

eg I have the string woo coo woo poo noo chho
then it should first split the string and then print the repeated words
and the number of times that word is printed
Posted
Comments
Sandeep Mewara 21-Feb-11 4:21am
   
Good to know. Whats stopping you? go ahead and write it.
Prerak Patel 21-Feb-11 4:23am
   
What is the problem, where are you stuck?

You didn't specify what you've tried so far. So I don't know if this is an exact answer to your question but here is my hint for you: you can use the standard std::map[^] container in order to simplify things a lot.

Here is a quick example for you:
C++
#include <iostream>
#include <sstream>
#include <string>
#include <map>

int main()
{
    // The test string
    std::string str("woo coo woo poo noo chho");
    
    // Count the number of occurrences for each word
    std::string word;
    std::istringstream iss(str);
    std::map<std::string, std::size_t> occurrences;
    while (iss >> word) ++occurrences[word];
    
    // Print the results
    for (std::map<std::string, std::size_t>::iterator it = occurrences.begin(); 
         it != occurrences.end(); ++it)
    {
        std::cout << "Word: " << it->first << "\t Occurrences: " << it->second << std::endl;
    }

    std::cin.get();
    return 0;
}

// ==== The program output is: ====
// Word: chho       Occurrences: 1
// Word: coo        Occurrences: 1
// Word: noo        Occurrences: 1
// Word: poo        Occurrences: 1
// Word: woo        Occurrences: 2


This program is only a demo but it might be useful for you.

If you have some problems please update your original question and/or post a comment but this time please be specific about your problem. :)
   
Comments
centinnel 21-Feb-11 7:17am
   
thanks it really works
Nuri Ismail 21-Feb-11 7:54am
   
You're welcome. :)
Member 13325284 2-Aug-17 5:37am
   
Consider this is the string:

string srch = "Sachin is a great player. Sachin has maximum century, Sachin has hundred century";

Now for the above string, with the above given solution we can find out that the words "Sachin" and "has" are repeated, but for the word "century", we are not able to detect as repeated words. Reason for this is, the word "century" is appended with comma separator, so it considers as "century," instead of just "century".

Is there anyway where we can resolve this?
Member 13325284 2-Aug-17 5:35am
   
Consider this is the string:

string srch = "Sachin is a great player. Sachin has maximum century, Sachin has hundred century";

Now for the above string, with the above given solution we can find out that the words "Sachin" and "has" are repeated, but for the word "century", we are not able to detect as repeated words. Reason for this is, the word "century" is appended with comma separator, so it considers as "century," instead of just "century".

Is there anyway where we can resolve this?
This[^] discussion might help you.
   

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