CPP
cpp
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
// Created by Ramisa Fariha
#include <iostream>
using namespace std;
//This function is used to compare the string arguments.
//It compares strings lexicographically which means it compares both the strings character by character.
int strcmp(char* s1,const char* s2)
{
// as long as *s1 == *s2, keep incrementing s1 and s2
for(; *s1 == *s2; s1++, s2++)
{
// *s1 is a null character (and hence *s2 is also a null character), return 0 implying that the strings are equal
if (*s1 == 0) {
return 0;
}
}
// if the execution reaches here, it means that *s1 != *s2
// return *s1 - *s2
// if *s1 is smaller (in ASCII value) than *s2, the returned value will be negative, implying that string s1 < string s2
// if *s1 is greater than *s2, the returned value will be positive, implying that string s1 > string s2
return (int)(*s1 - *s2);
}
int main()
{
char string1[20]="ChandlerBing";
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run