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 <stdio.h>

int streql (char *str1, char *str2)
{
while ((*str1 == *str2) && (*str1))
{
str1++;
str2++;
}
return ((*str1 == NULL) && (*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"?

Answer:
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.
while ((*str1 == *str2) && (*str1))
{
str1++;
str2++;
}
return ((*str1 == NULL) && (*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) && (*str2 == 0).
I found your question under immigration? Try putting it under another category. There is one for computer or programming. Signed COBOL programmer.

The immigration information post by website user , MyTend.com not guarantee correctness

  • how cheap is cheap imported labor and why we have it?
  • Is there an official statistic for how many people are deported for immigration violations?
  • Why don't day loborers consider US citizenship?
  • how ez/hard is it to become a canadian citizen w/all their benefits?
  • Is Immigration or the Laws of Spain more lenient than the ones in US, Canada or Australia?
  • Nicaragua Citizenship?
  • Where can I find information on Canadian Immigration Laws?
  • Can we all just Deport George Soros?
  • Is there any solution to my problem or not- Someone pliz help me coz i have no idea wat to do anymore.?
  • How much time it takes for USCIS o change my status from F2 to F1? I am in US and want to change it within us