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]);
}
}
}
}