3
My function needs to lexicographically calculate the value of the characters (by the ASCII table) of the string. For example:
"Alibaba" and "Came"
'A' = 65 and 'V' = 86
65 - 86 = -21.
Then the function would return l1
and if it were the same as the first letter it would go to the next letter. If all letters are equal, it would return 0. But it is always returning l2
. I’m making the wrong calculation?
My code:
void str_comparacao(char str1[], char str2[])
{
int i=0;
int y;
while(str1[i] != '\0' && str2[i] != '\0')
{
while(str1[i] != str2[i])
{
if(str1[i]==str2[i])
{
y=str1[i]-str2[i];
}
i++;
}
i++;
}
if(y<0)
printf("L1, str1 < str2\n");
else
printf("L2, str1 > str2\n");
}
Thank you, but I was wondering how you would do that in writing. Type like this, the 'V' of red and green are equal, then went to the next letter and so on, until arriving in a different letter and calculating the difference between the values of the letters, understood?
– flavio
@Yes, I understand, and that’s exactly what this code does!
– Victor Stafusa
A is, I did the wrong example kkkk, thanks bro, helped mto <3
– flavio