This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p02_1708.c
84 lines (77 loc) · 1.43 KB
/
p02_1708.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
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
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 02/1708
// String Comparison and Concatenation
#include <stdio.h>
void compare(char s1[50], char s2[50]);
void concat(char s1[50], char s2[50]);
void main()
{
char s1[50], s2[50];
int ch;
printf("\nEnter String 1: ");
scanf("%s", s1);
printf("Enter String 2: ");
scanf("%s", s2);
printf("\n1. Lexicographic Compare");
printf("\n2. Concatenate");
printf("\nEnter choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: compare(s1, s2); break;
case 2: concat(s1, s2); break;
default: printf("Wrong choice!\n");
}
printf("\n");
}
void compare(char s1[50], char s2[50])
{
//Assuming the strings are purely alphabetic
//Lexicographic and case sensitive comparison.
//strcmp()
int i;
printf("\n");
for(i = 0; s1[i] != '\0' && s2[i] != '\0'; i++)
{
if(s1[i] > s2[i])
{
printf("Not equal.\n%s comes before %s.\n", s2, s1);
return;
}
else if(s1[i] < s2[i])
{
printf("Not equal.\n%s comes before %s.\n", s1, s2);
return;
}
else
{
continue;
}
}
if(s1[i] == s2[i])
{
printf("Equal Strings. %s = %s\n", s1, s2);
}
else if(s1[i] == '\0')
{
printf("Not equal.\n%s comes before %s.\n", s1, s2);
}
else
{
printf("Not equal.\n%s comes before %s.\n", s2, s1);
return;
}
}
void concat(char s1[50], char s2[50])
{
int i, j;
for(i = 0; s1[i] != '\0'; i++);
for(j = 0; s2[j] != '\0'; i++, j++)
{
s1[i] = s2[j];
}
s1[i] = '\0';
printf("\n%s", s1);
}