Doubt in code (my if condition in while loop is not working)

//Trying to find largest word in string.
#include
#include
#include
using namespace std;

int main()
{
int n,m;
char s[100]=“my name is tushar”;
char a[100];
char*ptr=strtok(s," “);
cout<<ptr<<endl;
m=strlen(ptr);
while(ptr!=NULL)
{
ptr=strtok(NULL,” ");
n=strlen(ptr);

  if(n>m)
    {
    strcpy(a,ptr);
    }

}
cout<<a<<m;
return 0;

}

Hey @tushartiwari
You also need to update m(size of the largest string), while you’re updating the largest string a inside your condition if(n>m)

still not working and it is showing segmentation fault

#include
#include<string.h>
#include
using namespace std;
int main() {
int n,m;
char a[100]=“my name is tushar”;
char b[100];
char*p=strtok(a," “);
m=strlen§;
while(p!=NULL)
{
p=strtok(NULL,” ");
n=strlen§;
if(n>m)
{
strcpy(b,a);
m=strlen§;
}
}
cout<<m<<a;
return 0;
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.