Skip to main content

OOPS CPP Program-10

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".


#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);
}

Comments

Popular posts from this blog

C-Programming with Record

C-Programming Programs Syllabus Click for full record  1.a) Write a C program to ask the user to enter one char ( Upper-Case letter) check  whether user  entered a Upper-case letter or not(by using relational and logical operators) and then if user has  entered a Upper-case letter  convert into a Lower-case letter? ( hint: Upper-case means capital letters,  use ASCII information to check for Upper-case and convert) b) Write a  C program to ask the user to enter two integers and apply all arithmetic operations on those  print the corresponding values?(hint : +,-,*,/,%)     c) Write a C program to Determine the ranges of char, short, int and long int variables both signed and  Unsigned   By using sizeof operator(ii) By printing appropriate values from standard header (limits.h )   2.a) Write a Program to Find the Roots of a Quadratic Equation using if else and Switch statements. b) Write a Program which Gener...

OOPS CPP Program-14

14. Write a program to find transpose of a given matrix of mxn size using unary operator overloading #include<iostream> using namespace std; class matrix {         int x[5][5],m,n; public:         matrix(){ }         matrix(int a,int b)         {                m=a;n=b;         }         void get();         void put();         matrix operator !(); }; void matrix::get() {         int i,j;         cout<<"\n Enter a matrix of order"<<m<<"x"<<n<<"\n";                for(i=0;i<m;i++)                    for(j=0;j<n;j++)                        ...

OOPS CPP Program-15

15. Write a program to concatenate one string to another using binary operator overloading. #include<iostream> #include<string.h> using namespace std; class str  {         char *st; public:         str(int len, char *s)         {                  int l=strlen(s);                st= new char[l];                st=s;          }         str()         {         }         void put();         str operator +(str); }; str str :: operator +(str s)  {  /*str abc; abc.st=strcat(st, s.st); return abc;*/         st=strcat(st, s.st);         return *this; } void str :: put() {   cout<<...