jeudi 2 novembre 2017

finding length of the longest sequence of identical consecutive characters using pointers

The below code is not working, if i include int i=0 after declaration of character pointer its working.can anyone specify the reason..or help me out..

#include <iostream>
#include <fstream>

using namespace std;

fstream ob("js.txt");

int Search(char *ch)
{
int count1=0,count2=0;
for(int i=0;ch[i]!='\0';i++)
{
    int j=0;
    while(ch[j]!='\0')
    {
        if(ch[i]==ch[j])
            count1++;
        j++;
    }
    if(count2<count1)
    {
        count2=count1;
    }
    count1=0;
}
return count2;
}

int main()
{
char *c;
cout<<"Enter a string: ";
cin>>c;
cout<<"Longest no. of identical consecutive characters: "<<Search(c)<<endl;
}`

Aucun commentaire:

Enregistrer un commentaire