-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_Q8.cpp
91 lines (74 loc) · 1.33 KB
/
08_Q8.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
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <string.h>
#pragma warning(disable:4996)
char Upper(char c)
{
if ('z' >= c && c >= 'a')
return c - 32;
else
return c;
}
// Compare two string regarless of size
int Str_cmpic(const char* s1, const char* s2)
{
int len = strlen(s1) < strlen(s2) ? strlen(s1) : strlen(s2);
char cur_s1, cur_s2;
for (int i = 0; i < len; ++i)
{
cur_s1 = Upper(s1[i]);
cur_s2 = Upper(s2[i]);
if (cur_s1 == cur_s2)
continue;
else
{
if (cur_s1 > cur_s2)
return 1;
else
return -1;
}
}
return 0;
}
int STr_ncmpic(const char* s1, const char* s2, size_t n)
{
char cur_s1, cur_s2;
for (int i = 0; i < n; ++i)
{
cur_s1 = Upper(s1[i]);
cur_s2 = Upper(s2[i]);
if (cur_s1 == cur_s2)
continue;
else
{
if (cur_s1 > cur_s2)
return 1;
else
return -1;
}
}
return 0;
}
int main()
{
char str1[100];
char str2[100];
int size;
int result;
printf("First string: ");
scanf("%s", str1);
printf("Second string: ");
scanf("%s", str2);
printf("Input size of comparing two string(0 is whole size): ");
scanf("%d", &size);
if (size == 0)
result = Str_cmpic(str1, str2);
else
result = STr_ncmpic(str1, str2, size);
if (result == 0)
printf("Two strings are same.\n");
else if (result > 0)
printf("%s > %s \n", str1, str2);
else
printf("%s < %s \n", str1, str2);
return 0;
}