Trying to make my own strcmp function in C

Trying to write my own strcmp function that will take two const char arrays
and check to see if they are equal if they are equal it will return 1 if its not
it will return false
ALSO WORTH MENTIONING I DEFINED MY OWN TRUE AND FALSE AND ALSO USED BOOL TYPEDEF FOR INT

#define true 1
#define false 0
typedef int bool;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  bool IsEqual(const str1[],const str2[]){
int str1counter=0;
bool notequal = false;
while(str1[str1counter]!=0){
    if(str1[str1counter] == str2[str1counter]){
        str1counter++;}
    else if(str1[str1counter] != str2[str1counter]){
        notequal = true;
    }
}
if(notequal == true){
    return false;
}else{
    return true;
}

}
/*
But when i use the function the screen is just stuck on the mouse cursor
*/
//Below is how i would use my own compare function
char first[]="admin";
char second[] = "admin";
printf(" %d",IsEqual(first,second));
1
2
3
4
5
6
7
8
while(str1[str1counter]!=0)
{
    if(str1[str1counter] == str2[str1counter]){
        str1counter++;}
    else if(str1[str1counter] != str2[str1counter]){
        notequal = true;
    }
}


Imagine that the strings are NOT the same.

When would the loop end? What makes the while loop end if the strings are not the same?
But even when the strings are the same the loop is just stuck what am I doing wrong??
The code above doesn't actually compile. If that's the code you think you're running, you're not.

bool IsEqual(const str1[],const str2[]){
You haven't stated what type str1 and str2 are.

Once you fix that, you'll find that when you pass two identical strings, it does work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

bool IsEqual(const char str1[],const char str2[])
{
  int str1counter=0;
  bool notequal = false;
  while(str1[str1counter]!=0)
    {
      if(str1[str1counter] == str2[str1counter])
	{
	  str1counter++;
	}
      else if(str1[str1counter] != str2[str1counter])
	{
	  notequal = true;
	}
    }
  if(notequal == true)
    {
      return false;
    }
  else
    {
      return true;
    }
}

int main()
{
  cout << IsEqual("beans", "beans");
}

Last edited on
I think am drunk I had a long day today :D Ahhh now the issue you pointed out earlier so the loop is stuck when 2 strings are not equal. how would you go around that :D
Actually don't tell me ill figure it out and come back

ok done works perfectly now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bool IsEqual(const char str1[],const char str2[]){
int str1counter=0;
bool notequal = false;
while(str1[str1counter]!=0){
    if(str1[str1counter] == str2[str1counter]){
        str1counter++;}
    else if(str1[str1counter] != str2[str1counter]){
        notequal = true;
        break;
    }
}
if(notequal == false){
    return true;
}

}
/**********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************/

int main()
{
    char first[]="ad";
    char second[] = "ad";
    printf(" %d",IsEqual(first,second));
}


if both strings different it will return 0. Thank you for the help though <3

Oops have another problem now ough.
If the second string so char second[] is something different to char first it will return 1
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <stdbool.h> // C99 has stdbool.h which defines bool, true, false

bool IsEqual(const char *a, const char *b) {
    for ( ; *a && *a == *b; ++a, ++b) ;
    return *a == *b;
}

int main() {
    char a[] = "admin", b[] = "admin";
    printf("%s\n", IsEqual(a, b) ? "true" : "false");
}

Last edited on
When the strings aren't equal, you don't increment the counter, so you keep comparing the same character over and over and over....

There are many other things wrong with this function.
Trying to write my own strcmp function
Your function returns less information than strcmp, which tells you which string is lexicographically first when they aren't equal. That's a helpful thing to know and it comes practically for free in the code.

I DEFINED MY OWN TRUE AND FALSE AND ALSO USED BOOL TYPEDEF
Thta's a very bad idea. I'm not even sure that redefining a built-in type is legal.

Line 2: str1counter is misleading since it counts for both strings.

Line 3: I find it helpful to always name boolean variable for the positive sense, so I'd call this equal and change the sense of the variable. After all, what does if (!notequal == false) mean?

Line 4: for loops are your friend. They separate the "loopy" code from the "do each time" code. This should be for (int counter=0; str1[counter]; ++counter) { ...

Line 7: If you get to the else, then that condition is guaranteed to be true.

Line 8: Once you know that the strings aren't equal, you should return immediately. In particular, you should stop comparing because you may have reached the end of str2, and going beyond could result in a memory error.

Lines 11-15. You could just return !notequal or, if using equal like I suggested, return equal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
bool IsEqual(const char str1[],const char str2[]){
int strcounter=0;
bool notequal = false;
while(str1[strcounter]!=0){
    if(str1[strcounter] == str2[strcounter]){
        strcounter++;}
    else if(str1[strcounter] != str2[strcounter]){
        strcounter++;
        notequal = true;

    }
}
if(!notequal){
    return true;
}

}
/**********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************/

int main()
{
    char first[]="ab";
    char second[] = "abz";
    printf(" %d",IsEqual(first,second));
}


When i write an example like this it returns 1 which means it is equal but they arent equal.
However when the char first [] has more letters then the char second[] array it returns false could you show me an example please where my code would work how i want it too. Because my head is hurting now :D
That's a very bad idea. I'm not even sure that redefining a built-in type is legal.

It's fine (if somewhat pointless) in C, banned in C++.

However when the char first [] has more letters then the char second[] array it returns false could you show me an example please where my code would work how i want it to

http://www.cplusplus.com/forum/beginner/271481/#msg1170364
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
bool IsEqual(const char str1[],const char str2[]){
int strcounter=0;
bool equal = true;

for(strcounter;str1[strcounter];strcounter++){
    if(str1[strcounter]!= 0&&str2[strcounter]!=0
       &&str1[strcounter] == str2[strcounter]){
    }
    else if(str1[strcounter] != str2[strcounter]){
       return !equal;
       }
}
return equal;



  
}


/**********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************/

int main()
{
    char first[]="gzf";
    char second[] = "gzfp";
    printf(" %d",IsEqual(first,second));
}


Still same result i give up lmao
Last edited on
You're not checking for null pointers at the start.

You're not checking str2 in the loop test.

strcmp returns:
0 if st2 == str2
-1 if str1 > str2
1 if str1 > str2

https://pubs.opengroup.org/onlinepubs/009695399/functions/strcmp.html
Last edited on
When you get to the end of str1, you return true, even if str2 still has characters in it.

Inside the loop, all you have to check for is whether the characters do NOT match. That because if str2 is shorter than str1, then you'll reach the end of str2, compare its terminating null byte to some non-null byte in str1, and they won't match.

So that leaves what to do when the loop ends because you reached the end of str1. In that case, just loop at str2[counter] if it is 0 then the strings are equal, otherwise str2 is longer than str1 and the are not equal.
Topic archived. No new replies allowed.