Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

How to implement the strcmp function in C

1 Answer

0 votes
#include <stdio.h>

int strcmp(const char* s1, const char* s2) {
	// compare s1 and s2
	for (; *s1 == *s2; s1++, s2++)
		if (*s1 == '\0') {
			return 0;
		}
	return *(unsigned char*)s1 < *(unsigned char*)s2 ? -1 : +1;
}

int main()
{
	char str1[32] = "c programming";
	char str2[32] = "c programming";

	printf("%d", strcmp(str1, str2));

	return 0;
}





/*
run:
         
0
      
*/

 



answered Dec 16, 2022 by avibootz

Related questions

2 answers 781 views
1 answer 154 views
2 answers 252 views
1 answer 56 views
56 views asked Jun 10, 2025 by avibootz
1 answer 112 views
1 answer 109 views
1 answer 87 views
87 views asked Aug 2, 2024 by avibootz
...