Answer to Question #249335 in C++ for Asjad

Question #249335
1. Write a program to find words in an English text and store them in the Binary Search Tree in alphabetical order. Comparison is to be done with respect to the words in the text. When the same word appears, a count of number of occurrences needs to be maintained. Finally, print() function is used to print all words in alphabetical order along with their count, that denotes the number of occurrences.



Note: a) Use strcmp() function .
b) Assume that only words are to be considered from the text without any punctuation marks.
c) Assume that the input text is given in the main function.
1
Expert's answer
2021-10-11T00:05:31-0400
#include<bits/stdc++.h>
using namespace std;


void printAn(string arr[], int n)
{
	unordered_map<string, vector<string>> map;
	for(int i = 0; i < n; i++)
	{
		string wd = arr[i];
		char ls[wd.n() + 1];
		strcpy(ls, wd.c_str());
		sort(ls, ls + wd.n() + 1);
		string newWord = "";
		for(int i = 0; i < wd.n() + 1; i++)
		{
			newWord += ls[i];
		}
		
		if (map.find(newWord) != map.end())
		{
			map[newWord].push_back(wd);
		}
		else
		{
			vector<string> words;
			words.push_back(wd);
			map[newWord] = words;
		}
	}
	
	unordered_map<string,vector<string>>::iterator it;
	for(it = map.begin(); it != map.end(); it++)
	{
		vector<string> values = map[it->first];
		if (values.n() > 1)
		{
			cout << "[";
			for(int i = 0; i < values.n() - 1; i++)
			{
				cout << values[i] << ", ";
			}
			cout << values[values.n() - 1];
			cout << "]";
		}
	}
}


int main()
{
	string arra1[] = { "aaa","bob","ccc","dad"};
	int n = sizeof(arra1) / sizeof(arra1[0]);
	printAn(arra1, n);
	
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS