Tuesday, 28 April 2015

Count the occurence of each word from given sentence string using LinkList & OOP concepts.


Problem Description:
Given a string sentence, separate the each word from given string and count the occurrence of each word in string sentence.

P.S. The program coded using Link List data structure & OOP concepts.

The Sample Input & Output of program:
Input: dil dil pakistan jaan jaan pakistan dil dil pakistan jaan jaan pakistan

Output:
      dil: 4
      pakistan: 4
      jaan: 4  

Code:
#include 
#include

using namespace std;

class node{
 public:
  node* next;
  string data;
 public:
  node(string str){
   data=str;
   next=NULL;
  }
};class list{
 node* head;
 public:
 list()
 {
  head=NULL;
 }
 void insert(string str)
 {
  if(head==NULL)
   head=new node(str);
  else
  {
   node* temp=new node(str);
   temp->next=head;
   head=temp;
  }
 }
 void print(){
  node* temp=head;
  while(temp!=NULL)
  {
   cout<data<next;
  }
 }
 bool exist(string str)
 {
  node* temp=head;
  while(temp!=NULL)
  {
   if(temp->data==str) return true;
   temp=temp->next;
  }
  return false;
 }
 int count(string str)
 {
  int counter=0;
  node* temp=head;
  while(temp!=NULL)
  {
   if(temp->data==str) counter++;
   temp=temp->next;
  }
  return counter;
 }
};
int main()
{
 string str="dil dil pakistan jaan jaan pakistan dil dil pakistan jaan jaan pakistan";
 list visited;
 string array[10];
 int i=0;
 string temp;
 list l;
 stringstream ssin(str);
 while (ssin.good() && i < str.length()){
  ssin>>temp;
  l.insert(temp);
  ++i;
 };
 //string temp2;
 stringstream ssin2(str);
 i=0;
 while (ssin2.good() && i < str.length()){
  ssin2>>temp;
  if(!visited.exist(temp))
   cout<

0 comments:

Post a Comment