My function as right?

Hello! I make a simple function for compare two strings, the function is like a strcmp(), of string.h. My function is:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int CompareStr(char* str1, char* str2){
    int i;
    if ( strlen(str1) != strlen(str2) ) {
        printf("As diferent!\n");
        return 1;
    }
    else {
        for (i = 0; i < strlen(str1); i++) {
            if (str1[i] == str2[i]) {
                printf("Equals!\n");
                return 0;
            }
            else {
                 printf("As diferent!\n");
                 return 1;
            }
        }
    }
    return 0;
}


This function as right? I tested thid and work it. Thanks.
Last edited on
try this xD
1
2
3
4
int strCmp(char str1[], char str2[])
{
	return strCmp(str1, str2) + 1;
}
Last edited on
@ascii
...and that is how you create a stack overflow. ;)

-Albatross
Last edited on
why?


That function keeps calling itself, without any conditions as to when it will (or won't) repeat.

As for your function, andrezc, don't you think it might be declaring victory a little bit early?
1
2
3
4
int main()
{
	return CompareStr("a rabbit hole", "a rabbit pole");
}
Equals!



-Albatross
Topic archived. No new replies allowed.