char *strcpy(char *dest, const char *src); //字符串拷贝 int strcmp(const char *s1, const char *s2); //字符串比较 size_t strlen(const char *s); //字符串长度 char *strcat(char *dest, const char *src); //字符串连接
strcpy.c
#include <stdio.h> #include <string.h> char *strcpy1( char *str1,const char *str2) { while(*str2 != '\0') { *str1 = *str2; str1++; str2++; } *str1 = '\0'; } int main(int argc,char *argv[]) { char a[10] = {0}; char b[10]; do{ gets(b); strcpy1(a,b); printf("a:%s\n",a); }while(strncmp(b,"quit",4)); return 0; }
strcmp.c
#include <stdio.h> #include <string.h> void strcmp1(const char *str1,const char *str2) { while((*str1 != '\0') && (*str1 == *str2)) { *str1++; *str2++; } int n = *str1 - *str2; if(n == 0) printf("%d same string\n",n); else if(n < 0) printf("%d str2 is bigger\n",n); else printf("%d str1 is bigger\n",n); } int main(int argc,char *argv[]) { char a[10],b[10]; do{ printf("please input string: \n"); gets(a); gets(b); strcmp1(a,b); }while(strncmp(a,"quit",4) != 0); return 0; }
strlen.c
#include <stdio.h> #include <string.h> int strlen1(const char *str) { int n = 0; while(*str != '\0') { n++; str++; } return n; } int main(int argc,char *argv[]) { char a[10]; do{ gets(a); printf("length:%d\n",strlen1(a)); }while(strncmp(a,"quit",4)); return 0; }
strcat.c
#include <stdio.h> #include <string.h> char *strcat1(char *str1,const char *str2) { while(*str1 != '\0') str1++; while(*str2 != '\0') { *str1 = *str2; str1++; str2++; } *str1 = '\0'; return str1; } int main(int argc,char *argv[]) { char *c; char a[100] = {"abc"}; char b[10] = {0}; fgets(b,10,stdin); c = strcat1(a,b); printf("a:%s",a); return 0; }