-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrings.c
97 lines (75 loc) · 1.87 KB
/
Strings.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
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
// Prototypes for the user defined functions
int str_length(char []);
int str_compare(char [], char []);
void str_concat(char [], char []);
int main()
{
// Declare the variables
char str[50];
char str1[50], str2[50];
char str_des[100], str_src[50];
int length, comp_res;
// Accept the string from the user to find the length
printf("Enter a string :");
gets(str);
// Invoke the function to find the length of the string
length = str_length(str);
// Print the length of the string
printf("The length of %s is %d\n",str,length);
// Accept two strings to compare
printf("\nEnter two strings for string compare :\n");
gets(str1);
gets(str2);
// Invoke string compare function to compare the str1 and str2 strings
comp_res=str_compare(str1,str2);
if (comp_res < 0)
{
printf("String \"%s\" is less than string \"%s\"\n",str1,str2);
}
else if (comp_res == 0)
{
printf("String \"%s\" is same as string \"%s\"\n",str1,str2);
}
else
{
printf("String \"%s\" is greater than string \"%s\"\n", str1,str2);
}
// Accept two strings for string concatenation
printf("\nEnter two strings for string concatenation :\n");
gets(str_des);
gets(str_src);
// Invoke string concatenation function
str_concat(str_des,str_src);
// Print the concatenated string
printf("The string after concatenation is \"%s\"\n", str_des);
return 1;
}
int str_length(char s[])
{
int i;
for(i=0;s[i]!='\0';i++);
return i;
}
int str_compare(char s1[], char s2[])
{
int i;
for(i=0;s1[i] != '\0' && s2[i] != '\0';i++)
{
if (s1[i] != s2[i])
{
return (s1[i] - s2[i]);
}
}
return (s1[i] - s2[i]);
}
void str_concat(char des[], char src[])
{
int i,j;
for(i=0;des[i] != '\0';i++);
for(j=0;src[j] != '\0';i++,j++)
{
des[i] = src[j];
}
des[i] = '\0';
}