字符串
2016-07-30 本文已影响7人
Isy
int main(int argc, const char * argv[]) {
int ch;
while((ch = getchar()) != EOF) {
putchar(ch);
}
printf("EOF\n");
return 0;
}
char a[][10] = {};
表示a[0] --> char [10]
char *a[]
a[0] --> char *
$ln -s a.out my
$busybox
size_t strlen(const char *s);
size_t mylen(const char *s) {
int cnt = 0;
while(s[idx] != '\0') {
idx++;
}
return idx;
}
int strcmp(const char *s1, const char *s2);
int mycmp(const char *s1, const char *s2) {
for(int i = 0; i < strlen(s1); i++) {
for (int j = 0; j < strlen(s2); j++) {
if (s1[i] == s2[j]) {
continue;
}
if (s1[i] > s2[j]) {
return 1;
}
if (s1[i] < s2[j]) {
return -1;
}
}
}
return 0;
}
int mycmp2(const char *s1, const char *s2) {
//int idx = 0;
//while(s1[idx] == s2[idx] && s1[idx] != '\0') {
// idx++;
//}
while (*s1 == *s2 && *s1 != '\0') {
s1++; s2++;
}
return *s1 - *s2;
//return s1[idx] - s2[idx];
}
char *strcpy(char *restrict dst, const char *restrict src);
//restrict 表示dst和src不能重叠
//把src的内容拷贝到ds
char *dst = (char*)malloc(strlen(src)+1);
strcpy(dst,src);
char *mycpy(char * restrict dst, const char * restrict src) {
int idx = 0;
// while(src[idx] != '\0') {
// dst[idx] = src[idx];
// idx++;
// }
// dst[idx] = '\0';
return dst;
char *rest = dst;
while(*dst++ = *src++) {
NULL;
}
*dst = '\0';
return rest;
}
查找字符
char *strchr(const char *s, int c); // 左找
char *strrchr(const char *s, int c); // 右找
查找字符串
char *strstr(const char *s1, const char *s2);
char *strcasestr(const char *s1, const char *s2); //忽略大小写