forked from siddhant3s/cs215
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq2.c
executable file
·107 lines (89 loc) · 1.94 KB
/
q2.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
98
99
100
101
102
103
104
105
106
#include<stdio.h>
char* strcat(char* dest, char* src)
{
while (*dest++);
dest--;
while (*dest++ = *src++);
return dest;
}
char* strncat(char* dest, char* src, int n)
{
while (*dest++);
dest--;
while ((--n>=0) && (*dest++ = *src++));
*dest='\0';
return dest;
}
int strcmp(char *s, char *t)
{
for ( ; *s == *t; s++, t++)
if (*s == '\0')
return 0;
return *s - *t;
}
char toupper(char c)
{
if (c>='a' && c<='z') return 'A'+(c-'a');
else return c;
}
int stricmp(char *s, char *t)
{
for ( ; toupper(*s) == toupper(*t); s++, t++)
if (*s == '\0')
return 0;
return toupper(*s) - toupper(*t);
}
void strcpy(char *s, char *t)
{
while (*s++ = *t++);
}
void strncpy(char *s, char *t,int n)
{
while ( --n>=0 && (*s++ = *t++));
}
size_t strlen(char *s)
{
char* t=s;
while (*t++);
t--;
return t-s;
}
void strrev(char* s)
{
size_t l=-1, i=0;
while (s[++l]);
for (i=0;i<l/2;++i)
{
char t=s[i];
s[i]=s[l-i-1];
s[l-i-1]=t;
}
}
int main(void)
{
const size_t MAXLEN=50;
char s1[50]="The First String";
char s2[]="Second String";
printf("\t s1=%s \n\t s2=%s \n",s1,s2);
printf("Concatanating s1 and s2 to s1. Now,\n");
strcat(s1,s2);
printf("\t s1=%s \n\t s2=%s \n",s1,s2);
printf("Reversing s2:\n");
strrev(s2);
printf("\t s1=%s \n\t s2=%s \n",s1,s2);
printf("Copying s2 to s1:\n");
strcpy(s1,s2);
printf("\t s1=%s \n\t s2=%s \n",s1,s2);
printf("Concatenating first 5 character from s2 to s1:\n");
strncat(s1,s2,5);
printf("\t s1=%s \n\t s2=%s \n",s1,s2);
printf("Reversing s1:\n");
strrev(s1);
printf("\t s1=%s \n\t s2=%s \n",s1,s2);
printf("The length of s1=%i\n",strlen(s1));
strcpy(s1,"ME");
strcpy(s2,"me");
printf("strcmp over s1 and s2=%i",strcmp(s1,s2));
printf("stricmp over s1 and s2=%i",stricmp(s1,s2));
return 0;
}