#include <iostream>
using namespace std;
struct list
{
char alphabet;
int count;
struct list *next;
};
typedef struct list node;
node *head=NULL;
void print_node()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->alphabet<<": ";
cout<<temp->count<<endl;
temp = temp->next;
}
}
void create_node() ///create 26 node for A-Z alphabet
{
int i;
for(i=0; i<26; i++)
{
node *temp=new node();
temp ->alphabet= 97+i; ///save alphabet
temp->count=0; ///set count =0;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
node *last=head;
while(last->next != NULL)
{
last = last->next;
}
last->next=temp;
}
}
}
void frequency(char str[])
{
int i=0;
while(str[i]!=NULL)
{
node *temp=head;
while(temp->next!=NULL)
{
if(temp->alphabet==str[i] || temp->alphabet-32==str[i]) ///compare and count frequency
{
temp->count++;
break;
}
temp= temp->next;
}
i++;
}
}
int main()
{
create_node();
char str[100];
cout<<"Please Enter Any String: ";
cin>>str;
frequency(str);
print_node();
return 0;
}
Output
Please Enter Any String: code to haven
a: 0
b: 0
c: 1
d: 1
e: 1
f: 0
g: 0
h: 0
i: 0
j: 0
k: 0
l: 0
m: 0
n: 0
o: 1
p: 0
q: 0
r: 0
s: 0
t: 0
u: 0
v: 0
w: 0
x: 0
y: 0
z: 0
Add your comment