-
Notifications
You must be signed in to change notification settings - Fork 1
/
strcmp.c
57 lines (55 loc) · 1.97 KB
/
strcmp.c
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* This program compares two given strings.
* I tried to make this program attractive as much as I can :)
* Sadly but no interesting story behind this program :(
* Maybe I'm feeling very reluctant to type :P
*/
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char str1[1000], str2[1000];
printf("\n----------------------\n");
printf(" STRING COMPARISON");
printf("\n----------------------\n");
printf("\nString-1: "); scanf("%s", str1);
printf("String-2: "); scanf("%s", str2);
// if two strings are unequal
if (strcmp(str1, str2)) {
// if the number of letters in the two strings are equal
if (strlen(str1) == strlen(str2)) {
printf("\nThere are %zu characters in both the string.\n", strlen(str1));
printf("\n ----- -----");
printf("\n STR-1 STR-2");
printf("\n ----- -----");
for (int i = 0; i < strlen(str1); i++) {
if (str1[i] == str2[i])
printf("\n '%c' = '%c'", str1[i], str2[i]);
else
printf("\n '%c' != '%c'", str1[i], str2[i]);
}
// if the number of letters in the two strings are unequal
} else {
printf("\nThere are %zu characters in String-1,", strlen(str1));
printf("\nand %zu characters in String-2.", strlen(str2));
}
printf("\n\nStrings are not equal\n\n");
// if the two strings are equal
} else {
// if the number of letters in the two strings are equal
if (strlen(str1) == strlen(str2)) {
printf("\nThere are %zu characters in both the string.\n",
strlen(str1));
printf("\n ----- -----");
printf("\n STR-1 STR-2");
printf("\n ----- -----");
for (int i = 0; i < strlen(str1); i++)
printf("\n '%c' = '%c'", str1[i], str2[i]);
// if the number of letters in the two strings are unequal
} else {
printf("\nThere are %zu characters in String-1,", strlen(str1));
printf("\nand %zu characters in String-2.", strlen(str2));
}
printf("\n\nStrings are equal\n\n");
}
return 0;
}