My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

A string of charater is given.Find the highest occurance of a character and display that character. eg.: INPUT: AEGBCNAVNEETGUPTAEDAGPE. OUTPUT: E or I,J(if equal occurance)

Wednesday, September 23, 2009

//to find the charater which is repeated most time in string
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{       clrscr();
    char ch;
    struct node
    {    char info;
        node *link;
    };
    node *start,*q,*p,*r;
    start=NULL;
    cout<<"enter the string: ";
    while((ch=getch())!='\r')
    {       cout<<ch;
        node *p=new node;
        p->info=ch;
        if(start==NULL)
        {    start=p;
            p->link=NULL;
        }
        else
        {       q=start;
            while(q->link!=NULL)
            {    q=q->link;
            }
            q->link=p;
            p->link=NULL;
        }
    }
    char record[50];
    int count[50],idx=0;
    for(int i=0;i<50;i++)
    {    record[i]=NULL;
        count[i]=0;
    }

    while(start!=NULL)
    {       p=start;
        ch=start->info;
        while(p!=NULL)
        {       q=p->link;
            if(q->info==ch)
            {       p->link=q->link;
                delete q;
                count[idx]++;
                continue;

            }
            p=p->link;
        }
        if(count[idx]==count[0])
        {    record[idx]=ch;

            idx++;
        }
        if(count[idx]>count[0])
        {       count[0]=count[idx];
            for(int i=1;i<50;i++)
            {    count[i]=0;
                record[i]=NULL;
            }

            idx=0;
            record[idx]=ch;
            idx++;
        }
        start=start->link;

    }
    cout<<"\nmaximum occurance numbers are: ";
    i=0;
    while(record[i]!=NULL)
    {    cout<<record[i]<<", ";
        i++;
    }

    getch();
}

0 comments: