Implement the following sequence of operations one by one
1. Make a linked list for 26 (a to z) english alphabets where each node consists a single alphabet, an integer data for frequency count (initially 0) and next pointer. This list should be sorted in ascending order according to the ASCII value of the alphabets.
2. Now you have given a string consists of english alphabets. Your task is to update the frequency count values of above list for each occurrence of alphabets inside the given string.
For example, if the string is “babaaedd” then the updated list should be like this:
3. Finally traverse the whole list and print each alphbets with their frequency one by one from a to z. Please follow the sample input-output format. Input string length is not more than 100.
Sample input
Mistcsedepartment
Sample output
a : 1
b : 0
c : 1
d : 1
e : 3
f : 0
g : 0
h : 0
i : 1
j : 0
k : 0
l : 0
m : 2
n : 1
o : 0
p : 1
q : 0
r : 1
s : 2
t : 3
u : 0
v : 0
w : 0
x : 0
y : 0
z : 0
#include <iostream>
#include<string>
using namespace std;
struct Node {
char item;
int key;
Node* next;
};
Node* insert(char element)
{
Node* node = new Node;
node ->item = element;
node ->key = 0;
node->next = NULL;
return node;
}
Node* stringToList(string st, Node* head)
{
head = insert(st[0]);
Node* current = head;
for (int i = 1; i < st.size(); i++) {
current ->next = insert(st[i]);
current = current ->next;
}
return head;
}
void AlphabetCount(string text, Node* head1){
char arr2[text.size()];
for(int i=0; i<text.size(); i++){
text[i] = tolower(text[i]);
}
cout<<"The string is "<<text<<endl;
cout<<text<<endl;
Node* currNode = head1;
int arr1[27];
for(int i=0; i<26; i++){
arr1[i] = 0;
}
string arr = " ";
for(int i=0; i<26; i++){
arr += currNode->item;
currNode = currNode->next;
}
int index=0;
for(int i=0; i<arr.size(); i++){
if(arr[i]==text[i]){
arr2[i]++;
cout<<i<<endl;
}
else{
arr2[i] = 0;
}
//cout<<index;
}
for(int i=0; i<26; i++){
cout<<arr[i]<<":"<<arr1[i]<<endl;
}
}
void display(Node* node)
{
Node* currNode = node;
while (currNode != NULL) {
cout << currNode->item <<":"<<currNode->key<<endl;
currNode = currNode->next;
}
}
int main()
{
string st = "abcdefghijklmnopqrstuvwxyz";
Node* headNode = NULL;
headNode = stringToList(st, headNode);
print(headNode);
AlphabetCount("mistcsedepartment",headNode);
return 0;
}
Comments
Leave a comment