-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstring-functions.c
76 lines (55 loc) · 1.46 KB
/
string-functions.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
/* Various string functions in C */
//pre-process fetching contents of library stdio.h which contains predefined functions in C
#include <stdio.h>
//str copy function self made
char *strcpynew(char *d, char *s){
char *saved = d;
while ((*d++ = *s++) != '\0');
return saved; //returning starting address of s1
}
char *strcatnew(char *d, char *s){
char *saved = d;
while(*d != '\0'){
*d++; //finding end of d string;
}
while((*d++ = *s++) != '\0');
// *d = 0;
return saved;
}
int strcmpnew(char *d, char *s){
while((*d == *s)){
if(*s == '\0'){
return 0;
}
*d++;
*s++;
}
return *d - *s;
}
int strlennew(char *s){
int total; //to increase range we can take unsigned long long int
while(*s != 0){
total++;
}
return total;
}
//default function that is run by C everytime
int main(){
//for STRCPY
char s1[] = "rahul"; //initializing strings
char s2[] = "arora"; //initializing strings
strcpynew(s1, s2);
printf("strcpy: %s\n", s1); //updated string after strcpy
//for STRCAT
char s3[] = "rahul"; //initializing strings
char s4[] = "arora"; //initializing strings
strcatnew(s3, s4);
printf("strcat: %s\n", s3); //updated string after strcat
//for STRCMP
char s5[] = "a"; //initializing strings
char s6[] = "a"; //initializing strings
printf("strcmp: %d\n", strcmpnew(s5, s6)); //updated string after strmp
//for counting
char s7[] = "rahul";
printf("strlen: %d\n", strlennew(s7));
}