-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
74 lines (68 loc) · 1.76 KB
/
string.c
File metadata and controls
74 lines (68 loc) · 1.76 KB
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
#include <stdio.h>
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
char *strchr(const char *s, int c);
/* int my_strlen(char s[]) */
int main(int argc, char *argv[])
{
char s1[50], s2[50], c;
int n, menu;
printf("Please make a selection: ");
printf("\n1. Compare two strings.");
printf("\n2. Compare partialy two strings.");
printf("\n3. Find a character location inside a string.");
scanf("%d", menu);
switch (menu)
{
case '1':
printf("\nPlease enter the first string: ");
scanf("%s", s1);
printf("\nPlease enter the second string: ");
scanf("%s", s2);
printf("\nCompare result is: %d", my_strcmp(s1, s2));
break;
case '2':
printf("\nPlease enter the first string: ");
scanf("%s", s1);
printf("\nPlease enter the second string: ");
scanf("%s", s2);
printf("\nNow enter the number of characters you wish to compare: ");
scanf("%d", n);
printf("\nCompare partialy result is: %d", my_strncmp(s1, s2, n));
break;
case '3':
printf("\nPlease enter the string you wish to search: ");
scanf("%s", s1);
printf("\nPlease enter the character you are looking for: ");
scanf("%c", c);
printf("\nCharacter location is %d", my_strchr(s1, c));
break;
default:
printf("\nYou must choose one of three choises.");
break;
}
}
int my_strcmp(char s1[], char s2[])
{
int i;
for (i = 0; s[i] == t[i]; i++)
if (s[i] == '\0')
return 0;
return s[i] - t[i];
}
int my_strncmp(char s1[], char s2[], size_t n)
{
int i;
for (i = 0; s[i] == t[i]; i++)
if (s[i] == '\0' || i >= n)
return 0;
return s[i] - t[i];
}
int my_strchr(char s[], int c)
{
int i;
for (i=0;s[i] != '\0';i++)
if (s[i] == c)
return i;
return -1;
}