Largest string by using 2 d array

i tried to solve this question by writing the 2 d array nd my code is -

#include
#include
#include
using namespace std;
void largest_string(char arr[][1000], int N);
int main()
{
char arr[100][1000];
int N;
cin>>N;
cin.get();
for(int row = 0; row<N; ++row)
cin.getline(arr[row], 1000);
largest_string(arr, N);
return 0;
}
void largest_string(char arr[][1000], int N)
{
int x, y = INT_MIN;
for(int row = 0; row<N; ++row)
{
if(strlen(arr[row])>y)
{
y=strlen(arr[row]);
x=row;
}
}
cout<<"The max length among the strings is "<<y<<endl<<"that string is "<<endl<<arr[x]<<endl;
return;
}

this is not giving me the desirable answer .
what is wrong in this code???

Here’s the updated code, Brother : https://ide.codingblocks.com/s/295816

1 Like


sahi to h answer!!
what is the problem

@prateek_5
sir its okay now this program is working but sir what was wrong in my code??
can’t we use strlen function inside if condition
i wrote -
if(strlen(arr[row])>y)
but in ur code rather than writing strlen u assingned its value to len nd u wrote -
if(len>y)

@chhavibansal
maam it is showing this

@pushkar24sharma: strlen() returns an unsigned integer type.

you have taken a signed value of ‘y’ (-ve here).

This ‘y’ gets converted to an unsigned integer (while comparison ) which could be a very large value and hence it threw an error.

so, a better approach would be to take y=0 and not y= INT_MIN.

also, we know the length of a string will never be negative ( strlen( ) >=0 :slight_smile: )

updated code : https://ide.codingblocks.com/s/296306 :slight_smile:

1 Like

@prateek_5
where can i help u
???
@pushkar24sharma
are u a TA?
i am confused!!!

@prateek_5 okay thank u sir

1 Like

@chhavibansal thank u maam. my doubt is resolved

1 Like

Pushkar asked the doubt to which I replied.

No, I am not a TA.

I am sorry to make you confused @chhavibansal .

OKay
cool
i was pretty confused
great that now that doubt has been answered