10. Write a program which prompts the user to enter a string and returns the length of the longest sequence of identical consecutive characters within the string using pointers to data members and member function. For example, in the string "aaaAAAAAjjB", the longest sequence of identical consecutive characters is "AAAAA".

// Length of the Longest Sequence of Identical Consecutive Characters
// Input a String in which one letter has highest continuous frequency

#include<iostream>
#include<string>
using namespace std;
class sequence
{
            char s[20];                                  
  public:
            void getdata();
            void find(sequence);
};
void sequence::getdata()
{
            cout<<"Enter a String: ";
            cin>>s;
}
void sequence::find(sequence ob)
{
            int len,max,i,j,k=0,count=1,pos;
            char ch[20];                
            int fr[20];
            sequence *pm=&ob;  // pm is the pointer to object
            len=strlen(pm->s); // pointer to object -> pointer to member
            for(i=0; i<len; i++)
            {
                        j=i+1;
                        if(s[i]==s[j]) count++;
                        else
                        {
                                    ch[k]=s[i];    // put character in array 'ch'
                                    fr[k]=count;    // put count in array 'fr'
                                    k++;     count=1;
                        }
            }
            for(i=0; i<k; i++)
                        cout<<ch[i]<<fr[i]<<" ";
            cout<<"\n";
            max=0;
            for(i=0; i<k; i++)  // finds maximum continuous frequency
                        if(fr[i]>max)
                        {
                                    max=fr[i];       pos=i;
                        }
            cout<<"Letter "<<ch[pos]<<" has the Longest Sequence: "<<max<<"\n";
}
int main()
{
            sequence seq;
 
            void (sequence::*pg)()=&sequence::getdata;
            (seq.*pg)();
            void (sequence::*pf)(sequence)=&sequence::find;
            sequence *op=&seq;
            (op->*pf)(seq);
}

OUTPUT:

Enter a String: aaaAAAAAjjB
a3 A5 j2 B1
Letter A has the Longest Sequence: 5

Enter a String: jjjK
j3 K1
Letter j has the Longest Sequence: 3

Enter a String: ssjjkkMMM
s2 j2 k2 M3


Letter M has the Longest Sequence: 3
Posted by Unknown On 03:29 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *