#include <string.h>
size_t strlen(const char *s);
strlen;
#include <stdio.h>
#include <string.h>
int my_strlen(char *s) {
int cunt = 0;
int i = 0;
while(s[i] != '\0') {
i++;
cunt++;
}
return cunt;
}
int main() {
char line[] = "hello world";
printf("strlen = %lu\n", strlen(line));
printf("sizeof() = %lu\n", sizeof(line));
printf("my_strlen = %d\n", my_strlen(line));
return 0;
}
strcmp
#include <stdio.h>
#include <string.h>
int my_cmp(char *s1, char *s2) {
/* int i = 0;
while(1) {
if (s1[i] != s2[i]) {
break;
}
else if (s1[i] == '\0') {
break;
}
i++;
}
return s1[i] - s2[i];
*/
while(*s1 == *s2 && *s1 != '\0') {
s1++;
s2++;
}
return *s1 -*s2;
}
int main() {
char s1[] = "abcdefg ";
char s2[] = "abcdefgh ";
printf("%d\n", strcmp(s1, s2));
printf("%d\n", my_cmp(s1, s2));
printf("%d\n", 'a'-'A');
printf("%d\n", ' ' - 'h');
return 0;
}
strcpy
strchr
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char s[] = "hello";
char *p = strchr(s, 'l');
printf("%s\n", p);
char *q = strchr(p + 1, 'l');
char c = *p;
*p = '\0';
char *t = (char*)malloc(strlen(p) + 1);
t = strcpy(t, s);
printf("%s\n", q);
printf("t = %s\n", t);
return 0;
}