-
-
-
-
-
- #include<iostream>
- #include<string>
-
- using namespace std;
-
- int countWords(const char* sent)
- {
- int count = 0;
- while(*sent != '\0')
- {
- if(*sent == ' ') count++;
- *sent++;
- }
- return count+1;
- }
-
- string longest(string words[], int length)
- {
- string longestWord = "";
- for(int i = 0; i < length; i++)
- {
- if(longestWord.length() < words[i].length())
- {
- longestWord = words[i];
- }
- }
- return longestWord;
- }
-
- int getWordSum(string st)
- {
- int sum = 0;
- const char* chr = st.c_str();
- while(*chr != '\0')
- {
- sum += *chr;
- *chr++;
- }
- return sum;
- }
-
- int main()
- {
- char* sent = new char[1024];
- cout<<"Enter a sentence:";
- cin.get(sent, 1024, '\n');
-
- int wordcount = countWords(sent);
- string words[wordcount];
-
- char* tok = strtok(sent, " ");
-
-
- int i = 0;
- while(tok != NULL)
- {
- cout<<tok<<"\n";
- words[i] = tok;
- i++;
- tok = strtok(NULL, " ");
- }
-
- cout<<"No. of words = "<<i<<"\n";
-
-
-
- cout<<"The longest word is:"<<longest(words, i)<<"\n";
-
-
-
- int wordValues[wordcount];
- for(int j = 0; j < wordcount; j++)
- {
- wordValues[j] = getWordSum(words[j]);
- }
-
- int smallest = 0;
- cout<<"HI\n";
- for(int k = 0; k<wordcount-1; k++)
- {
- smallest = k;
- cout<<"xeced";
- for(int l = k+1; l<wordcount;l++)
- {
- if(wordValues[l] < wordValues[smallest])
- smallest = l;
-
- }
- string tmp = words[smallest];
- words[smallest] = words[k];
- words[k] = tmp;
-
- int tmpV = wordValues[smallest];
- wordValues[smallest] = wordValues[k];
- wordValues[k] = tmpV;
- }
- cout<<"Words in ascending order:\n";
- for(int j = 0; j<wordcount; j++)
- cout<<words[j]<<endl;
-
-
- for(int x = 0; x < wordcount; x++)
- {
- int count = 1;
- for(int y = x+1; y < wordcount; y++)
- {
- if(words[x] == words[y] && !(words[x] == "\0"))
- {
- count++;
- words[y] = "\0";
- }
- }
- cout<<"Frequency of "<<words[x]<<":"<<count<<endl;
- }
-
-
- return 0;
- }