#include <iostream>
usingnamespace std;
int main ()
{
char mat[100][100][100], t[100];
int n, m, i, j, max=0;
cout << "n= "; cin >> n;
cout << "m= "; cin >> m;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
cout << "mat[" << i << "][" << j << "]= "; cin >> mat[i][j];
}
}
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
if (max>strlen (mat[i][j]))
{
strcpy (max, strlen(mat[i][j]));
strcpy (t, mat[i][j]);
}
cout << t << endl;
}
}
The problem is that the compiler cannot compare two different types as you know (max>strlen (mat[i][j])) and i don't know how to change it. Any tip would be appriciated! Thank you!
By the way, i work in mingw.
But I think this is not the problem. strlen works for NULL terminated characters, which means, your string has to end with NULL for strlen to work, here's an example:
Now if you use strlen with this, you'll get a wrong answer. To get the right answer, you have to add this line too:
mystr[5] = NULL;
Your array doesn't differ conceptually whatsoever from an int array except that char's size is 1 byte. Can you ever measure the length of an array of ints? not really! So to do it, you have to terminate your string with a NULL like I mentioned. It's some kind of a special hack to make strings defined in size.