Longest string in an array in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Longest string in an array in C++

I wrote this code: char a[100][100] = {"solol","a","1234567","123","1234"}; int max = -1; for(int i=0;i<5;i++) if(max<strlen(a[i])) max=strlen(a[i]); cout<<max; The output it gives is -1. But when I initialize the value of max by 0, the code works fine. Why is it so?

26th Mar 2018, 2:45 PM
Muktadir Khan
1 Answer
+ 2
Nevermind, I fixed my code myself. I was comparing signed value with an unsigned value returned by strlen(a[i]);
26th Mar 2018, 3:06 PM
Muktadir Khan