Input:
Today I eat bread. Today I eat cookies.
Output:
eat: 2
I: 2
Today: 2
bread: 1
cookies: 1
I have to make a program that counts the number of times that a word occurs in the input. Then if the number of times is equal between some words, then I display them in alphabetic order. Until now I did this:
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int countt (string text);
int main () {
string text;
while (getline(cin,text)) //Receive the input here
countt(text); //Send the input to countt
return 0;
}
int countt (string text) {
int i,j;
string aux; //I make a string aux to put the word to compare here
for (std::string::const_iterator i = text.begin(); *i != ' '; i++){
for (std::string::const_iterator j = aux.begin(); j != text.end(); j++)
*j=*i; //But here an error is given: 25:9: error: assignment of read-only location ‘j.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*<const char*, std::basic_string<char> >()’
}
}
Many thanks in advance.