🗽
🗽
🗽
🗽
C/Cpp and OS
Search…
Guide
Templates and methods
Linked List with C
Linked List with C++
Stacks with C
Queues with C
Graphs with C/C++
Trees with C/C++
DS with C
Reverse Linked List
Implement Queue with LL
Find mid of LL without traverse
Delete a Node from LL
Create a Binary search tree
String and Array
Reverse string
implement strcmp in C
Maximum Subarray
CTCI
Ch 01 - Arrays and Strings
Ch 02 - Linked List
Algorithm
Bubble Sort
Insertion Sort
Merge Sort
Count Prime
Basic Concept in C
Understand about pointer
Pre/Post Increment Operator
string and constant charaters
buffer overflow
Short-circuit evaluation
Allocate memory for 2D array dynamically
OS review
For beginner
Reference
Question Collecting:
References
Template on Linux
Powered By
GitBook
implement strcmp in C
Better version
1
#include <stdio.h>
2
3
int z_strcmp (char* str1, char* str2){
4
5
printf("str1: %s, str2: %s\n", str1, str2);
6
while( *str1!= '\0' && *str1 - *str2 == 0){
7
str1++;
8
str2++;
9
}
10
return *str1 - *str2 != 0 ? -1: 0;
11
}
Copied!
Worse version
1
#include <stdio.h>
2
3
int _strcmp (char* str1, char* str2){
4
char* ptr1 = str1;
5
char* ptr2 = str2;
6
7
printf("str1: %s, str2: %s\n", str1, str2);
8
while( *ptr1!= '\0' && *ptr2 != '\0'){
9
if(*ptr1 != *ptr2){
10
return -1;
11
}
12
ptr1++;
13
ptr2++;
14
}
15
if(*ptr1 - *ptr2 == 0){
16
return 0;
17
}
18
return -1;
19
}
Copied!
Test code
1
int main() {
2
printf("result: %d\n", z_strcmp("","accccc"));
3
return 0;
4
}
Copied!
String and Array - Previous
Reverse string
Next - String and Array
Maximum Subarray
Last modified
5mo ago
Copy link
Contents
Better version
Worse version