Thursday, July 30, 2009

Can a C programmer help me?

I don't understand this function called "streql" (it tests if 2 strings are equal, and it returns 1 if they are, and 0 if not...)





Here is the code, in C:








#include %26lt;stdio.h%26gt;





int streql (char *str1, char *str2)


{


while ((*str1 == *str2) %26amp;%26amp; (*str1))


{


str1++;


str2++;


}


return ((*str1 == NULL) %26amp;%26amp; (*str2 == NULL));


}





void main (void)


{


printf("Testing Abc si Abc %d\n", streql("Abc", "Abc"));


printf("Testing abc si Abc %d\n", streql("abc", "Abc"));


printf("Testing abcd si abc %d\n", streql("abcd", "abc"));


}








Can you explain me what is the logic of what is written in the "while", and what is the logic of what is written inside the "return"?

Can a C programmer help me?
Look at the while loop


its condition is to check if the 2 pointers point to equal values.


also it checks if the first pointer is not equal to ZERO which means that the first string has ended. if the condition comes true then advance to the next char for both strings. the return statement checks if both strings had ended. if both ended then they match





Take the following example,


"ABCD", "ABCD"





at first *str1 = 'A' and *str2 = 'A' and *str1 != NULL, so advance


*str1 = 'B' and *str2 = 'B' and *str1 != NULL, advance,


.


.


.


at the end *str1 equals NULL after finishing the 'D' check then the while loop terminates


then the return statement checks if both pointers point to NULL (the two strings finished then it returns true.
Reply:A string of characters is a sequence of 0 or more ASCII characters, sequence which the C language ends with the character called NULL (ASCII 0). Report It

Reply:The difference between 'A' and "A":





--- --- --- --- --- --- --- ---


A A \0


--- --- --- --- --- --- --- ---


'A' "A" Report It

Reply:I found your question under immigration? Try putting it under another category. There is one for computer or programming. Signed COBOL programmer.
Reply:while ((*str1 == *str2) %26amp;%26amp; (*str1))


{


str1++;


str2++;


}


return ((*str1 == NULL) %26amp;%26amp; (*str2 == NULL));





str1 and str2 are pointers. The character pointed to moves one to the right each time you execute str1++ or str2++, respectively. "*str1" means what character the pointer str1 is currently pointing at. So the while statement says, while the character pointed to by str1 is the same as the character pointed to by str2 AND while the character pointed to by str1 is not 0 (meaning is not the null character that terminates string str1), then increment the pointers str1 and str2 to go to the next character.





The return statement means to return true if the thing pointed to by str1 is NULL and the thing pointed to by str2 is NULL.





This is a defective function, as far as I'm concerned. You should exit the while loop as soon as there is a difference found in the two strings or as soon as you get to the end of str1. But at that point *str1 should be 0 and *str2 should be 0 if the strings are equal. str1 and str2 are pointers, but *str1 and *str2 are NOT pointers, they are characters. To compare characters to NULL may work, but it is very bad programming. The return statement should say return (*str1 == 0) %26amp;%26amp; (*str2 == 0).


No comments:

Post a Comment