Hi

I have a question.

Basically I want to create a dictionary of words, where a user has typed in a couple of sentencies. Im then supposed to sort it in alphabetic order and also count how many times a word has appeard. The sentence contains letters and whitespaces.

Ex: hi i am typing in a sentence so i can sort it in alphabetic order

a 1
am 1
alphabetic 1
hi 1
i 2
in 2
it 1
order 1
sentence 1
so 1
sort 1
typing 1


The first thing i did was to put the sentence the user typed in a vector, so each word has its own index in the vector.

the next thing I did was to create a function that sorted the words. But I based it on
strcmp and it doesnt support parameters just constant values, as I discovered after writing the function.

So my question is now should I dump strcmp? if so can I do it in a similar way by comparing index?


I submit the code, dont get to detailed in the function because its the first draft.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cstring>
 
using namespace std;

 
int main(void)
{
  string sentence, word;
  // create a string vector of the words the user typed in
  vector<string> UserVector;
  vector<string> StoreWords;
  vector<string> s1;
 
  cout << "Enter a sentence: ";
  getline(cin, sentence);
 
  // put the sentence into a stream
  istringstream in(sentence);


 //Putting the sentence into a vector
 while (in >> word)
  {
   UserVector.push_back(word);
  }


 
  return 0;
}

void insert(vector<string> s, vector<string> v)
{


for (int j=0; j<=UserVector.size(); j++)
{


	for (int i=0; i<=StoreWords.size(); i++)
	{
		if (strcmp(v[j], s[i]) > 0)
			break;
		else if (strcmp(v[j], s[i]) = 0) 
		{
			s = s1;
			s.resize(i); 
			s.push_back(v[j]);
				
				for (int u=0; u<10; u++)
				{
					s.push_back(s1[u]);
				}
		}

		else (strcmp(v[j], s[i]) < 0)
		{
			
			s = s1; 
			s.resize(i);
			s.push_back(v[j]);
				
				for (int u=0; u<10; u++)
				{
					s.push_back(s1[u]);
				}
			
		}
		
	}



}

Recommended Answers

All 5 Replies

1) I would create a structure that holds the count and string for each word

struct words
{
    std::string word;
    int count;
}

Now have a vector of these structures vector<words> wordList; Now when you get a word, search the vector for occurence. It its already in the vector just increment the counter. If not, add a structure to the vector.

as for using strcmp() -- dump it. You don't need it because you can use std::string's == operator to determine if the two strings are equal. And its not necessary to sort the words if you use my suggestion above.

I think the right solution is std::map<std::string,int> (another name of map is dictionary).
Fast search, automatic sorting + Ancient Dragon's counter - three in one solution.

ok ive changed my code, I have got ride of strcmp() and Ive added a struct for the wordlist.

The question is now when i want to put another word in the dictionary (the last step in the function insert), do I need to create a struct for the UserVector because I have an ordianry vector now?

But the major question is if Ive constructed the program correctly from the beginning cuz I cant help feeling its something fundamentally wrong.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cstring>
 
using namespace std;


void insert(vector<string> a, vector<string> b)
{


//A loop for the words the user typed in
for (int j=0; j <= b.size(); j++)
{
	//A loop for the dictionary, where it searches through. a.size is the size of the current dictionary
	for (int i=0; i <= a.size(); i++)
	{
		//Compares if the word the user typed in whit the word in the dictionary. If its bigger, loop again
		if (b[j] > a[i].Ord)
		{
			break;
		}
		//Is the two strings equal?, if so Antal++
		else if (b[j] == a[i].Ord)
		{
			a[i].Times++;
		}
		//Putting the word in the dictionary....might be some thinking error......
		else
		{
			a = s1; 
			a.resize(i);
			a.push_back(b[i]); 
				for (int k=; k<=s1.size; k++)
				{
					a.push_back(s1[k]);
				}
		}
	}

}

} 
int main(void)
{
	string sentence, word;
	// create a string vector of the words the user typed in
	 vector<string> UserVector;
   	 vector<string> s1;
 
	cout << "Enter a sentence: ";
	getline(cin, sentence);
 
	// put the sentence into a stream
	istringstream in(sentence);

	//Putting the sentence into a vector........ I think I have to create a structure for this...how?
	while (in >> word)
	 {
		UserVector.push_back(word);
	 }


	struct OrdLista
	{
		string Ord;
		int Times;
	};

	//Now we have a vector whit structures
	vector<OrdLista> Vektor;




	int p = 0; 
	cin >> p; //Push 0 to end

 
  return 0;

Don't waste time on these vector based solution debugging troubles.

typedef std::map<std::string,int> Dictionary;
typedef Dictionary::iterator Diter;
typedef Dictionary::const_iterator Citer;
typedef std::pair<std::string,int> V;

// I don't like to inherit from STL classes...
struct Dict
{
    Dict& ins(const std::string& word);
    Dict& ins(const char* pword) 
    {
        if (pword)
            ins(std::string(pword));
        return *this;
    }
    Dictionary dict;
};
// Increment counter if the word found.
Dict& Dict::ins(const std::string& word)
{
    Diter d = dict.find(word);
    if (d != dict.end())
        (*d).second++;
    else // new word
        dict.insert(V(word,1));
    return *this;
}

int main()
{
    Dict d;
    d.ins("vector").ins("all").ins("problems");
    d.ins("avoided").ins("no").ins("vector");
    for (Citer p = d.dict.begin(); p != d.dict.end(); ++p)
        std::cout << (*p).second << '\t' << (*p).first << '\n';
    std::cout.flush();
    // If you want vector of word+counter, here it is:
    std::vector<V> v(d.dict.begin(),d.dict.end());
    std::cout << '=' << v.size() << std::endl;
    return 0;
}
/* Output:
1       all
1       avoided
1       no
1       problems
2       vector
=5
*/
commented: Excellent usage of the STL components! +4

Ty ArkM!

Ive learned alot just looking at your code and study the parts in my book...I havent covered some stuff that you use, but I understand it and ive learned alot. I still did my own longer "version" ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.