strcpy函数
- 原型:
char * strcpy(char * destin, const char * source)
- 作用:把source指向的字符串拷贝到destin指向的字符串中
- 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| char * my_strcpy(char * destin, const char * source) { assert(destin != NULL && source != NULL); char *tmp = destin;
do { *destin++ = *source++ }while(*destin && *source);
return tmp; }
int main() { char destination[100] = {1}; const char *source = "abcdefgh";
char *destion = my_strcpy(destination, source); printf("%s\n", destion);
return 0; }
|
strncpy函数
- 原型:
char * strncpy(char * str1, char * str2, int count)
- 作用:把str2指向的前count个字符拷贝到str1指向的字符串中
- 代码:
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
| #include <stdio.h> #include <string.h> #include <assert.h>
char * my_strncpy(char * str1, char * str2, int count) { assert(str1 != NULL); while(count && (*str1++ = *str2++)) { count--; } while(count--) { *str1++ = '\0'; } return str1; }
int main() { char destination[10] = {0}; char *source = "abcdefgh"; int count = 5;
my_strncpy(destination, source, count); printf("%s\n", destination); return 0; }
|
strcmp函数
- 原型:
int strcmp(const char * str1, const char * str2)
- 作用:比较str1和str2,str1 > str2返回1,str1 == str2返回0
- 代码:
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
| int my_strcmp(const char * str1, const char * str2) { while( ! ((*str1 != *str2) && str1 != '\0')) { str1++; str2++; } return (*str1 - *str2); }
int main() { char *buf1 = "aaaa", *buf2 = "aaaab", *buf3 = "aaaac"; int ptr = 0;
ptr = my_strcmp(buf2, buf1); if (ptr > 0) printf("string 2 is bigger than string 1\n"); else printf("string 2 is smaller than string 1\n");
ptr = my_strcmp(buf2, buf3); if (ptr > 0) printf("string 2 is bigger than string 3\n"); else printf("string 2 is smaller than string 3\n");
return 0; }
|
strncmp函数
- 原型:
int strncmp(const char * str1, const char * str2, int count)
- 作用:比较str1和str2的前n个字符
- 代码:
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
| int my_strncmp(const char * str1, const char * str2, int count) { if(!count) return 0; while(--count && *str1 && *str1 == *str2) { str1++; str2++; } return (*str1 - *str2); }
int main() { char *str1 = "China is a nation!"; char *str2 = "French is a nation!"; int count = 5, ptr = 0;
ptr = my_strncmp(str1, str2, count); if(ptr != 0) printf("str1 is not equal to str2!\n"); return 0; }
|
stricmp函数
- 原型:
int my_stricmp(const char *str1, const char *str2)
- 作用:不区分大小写的比较str1和str2
- 代码:
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
| int my_stricmp(const char *str1, const char *str2) { char ch1 = '0', ch2 = '0';
do { if((ch1 = (unsigned char)(*(str1++))) >= 'A' && ch1 <= 'Z') ch1 += 0x20; if((ch2 = (unsigned char)(*(str2++))) >= 'A' && ch2 <= 'Z') ch2 += 0x20; }while(ch1 && (ch1 == ch2)); return (ch1 - ch2); }
int main() { char *str1= "ammana"; char *str2 = "bibi"; char *str3 = "AMMANA"; int ptr = 0, ptr1 = 0;
ptr = my_stricmp(str1, str2); if(ptr > 0) printf("str1 bigger than str2!\n"); else printf("str1 smaller than str2!\n");
ptr1 = my_stricmp(str1, str3); if(ptr1 > 0) printf("str1 bigger than str3!\n"); else if(ptr1 < 0) printf("str1 smaller than str3!\n"); else printf("str1 equal to str3!\n");
return 0; }
|
strlen函数
- 原型:
unsigned int strlen(const char * str)
- 作用:计算str的长度并返回
- 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| unsigned int my_strlen(const char * str) { unsigned length = 0; while(*str != '\0') { length++; str++; } return length; }
int main() { char * str = "abcdefgh"; int len = 0;
len = my_strlen(str);
printf("%d\n", len); return 0; }
|
strcat函数
- 原型:
char * strcat(char* destin, const char *source)
- 作用:连接两个字符串,将source连接到destin
- 代码:
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
| char * my_strcat(char* destin, const char *source) { char * temp = destin;
while(*temp) { temp++; }
while(*temp++ = *source++) {};
return temp; }
int main() { char destination[25]; char *space = " ", *String = "C++!", *Hello = "Hello";
strcpy(destination, Hello); my_strcat(destination, space); my_strcat(destination, String);
printf("%s\n", destination); return 0; }
|
strchr函数
- 原型:
char * strchr(char * str, const char c)
- 作用:查找str中c首次出现的位置,并返回位置或者NULL
- 代码:
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
| char * my_strchr(char * str, const char c) { while(*str != '\0' && *str != c) { str++; } return (*str == c ? str : NULL); }
int main() { char * string = "abcdefgh"; char c = 'd', *ptr = NULL,
ptr = my_strchr(string, c); if(ptr) printf("%c\n", c); else printf("Not Found!\n");
return 0; }
|
strrchr函数
- 原型:
char * strrchr(char * str, const char c)
- 作用:查找str中c最后一次出现的位置,并返回位置或者NULL
- 代码:
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
| char * my_strrchr(char * str, const char c) { char * end = str + strlen(str);
while(*str != *end && *end != c) { end--; }
if(*end == *str && *end != c) return NULL;
return end; }
int main() { char * string = "abcdefgh"; char c = 'd', *ptr = NULL,
ptr = my_strrchr(string, c); if(ptr) printf("%c\n", c); else printf("Not Found!\n");
return 0; }
|
strrev函数
- 原型:
char * strrev(char * str)
- 作用:翻转字符串并返回字符串指针
- 代码:
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
| char * my_strrev(char * str) { assert(str != NULL); char *end = str; char *head = str; char temp = '0';
while(*end++) {};
end--; end--;
while(head < end) { temp = *head; *head++ = *end; *end-- = temp; } return str; }
#if 0
char * my_strrev(char * str) { assert(str != NULL); char *end = strlen(str) + str -1; char temp = '0';
while(str < end) { temp = *str; *str = *end; *end = temp; str++; end--; } return str; } #endif
int main() {
char str[] = "Hello World";
printf("Before reversal: %s\n", str); my_strrev(str); printf("After reversal: %s\n", str); return 0; }
|
strdup函数
- 原型:
char * strdup(const char * str)
- 作用:拷贝字符串到新申请的内存中返回内存指针,否则返回NULL
- 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <assert.h>
char * my_strdup(const char * str) { char * temp = (char*)malloc(strlen(str) + 1); assert(str != NULL && temp != NULL); strcpy(temp, str); return temp; }
int main() { char *str = NULL, *string = "abcde";
str = my_strdup(string); printf("%s\n", str); free(str);
return 0; }
|
strstr函数
- 原型:
char * strstr(const char * str1, char * str2)
- 作用:查找str2在str1中出现的位置,找到返回位置,否则返回NULL
- 代码:
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
| char * my_strstr(const char * str1, char * str2) { assert(str1 != NULL & str2 != NULL); int len1 = strlen(str1); int len2 = strlen(str2);
while(len1 >= len2) { len1--; if(!strncmp(str1, str2, len2)) { return str2; } str1++; } return NULL; }
int main() { char *str1 = "China is a nation!", *str2 = "nation", *ptr = NULL;
ptr = my_strstr(str1, str2); printf("The string is: %s\n", ptr); return 0; }
|
strpbrk函数
- 原型:
char *strpbrk(const char *str1, const char *str2)
- 作用:从str1的第一个字符向后检索,直到’\0’,如果当前字符存在于str2中,那么返回当前字符的地址,并停止检索.
- 代码:
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
| #include <assert.h>
char *my_strpbrk(const char *str1, const char *str2) {
assert(str1 != NULL && str2 != NULL); const char *str3 = str2;
while(*str1) { for(str3 = str2; *str3; ++str3) { if(*str1 == *str3) break; } if(*str3) break; str1++; }
if(*str3 == '\0') str1 = NULL; return (char*)str1; }
int main() { char s1[] = "http://see.xidian.edu.cn/cpp/u/xitong/"; char s2[] = "see"; char *p = my_strpbrk(s1, s2); if(p) { printf("The result is: %s\n",p); } else { printf("Sorry!\n"); } return 0; }
|
strspn函数
- 原型:
int my_strspn(const char *str1, const char *str2)
- 作用:从参数str1字符串的开头计算连续的字符,而这些字符都完全是str2所指字符串中的字符。
- 代码:
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
| #include <assert.h>
int my_strspn(const char *str1, const char *str2) {
assert(str1 != NULL && str2 != NULL); const char *tmp = str1; const char *str3;
while(*tmp) { for(str3 = str2; *str3; ++str3) { if(*str3 == *tmp) break; } if(*str3 == '\0') break; tmp++; }
return tmp - str1; }
int main() { char s1[] = "hello world!"; char s2[] = "i am lihua";
int p = my_strspn(s1, s2);
printf("The result is:%d\n", p); return 0; }
|
strtok函数
- 原型:
char *my_strtok(char *sou, char *delim)
- 作用:将字符串分割成一个个片段
- 代码:
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
| static char *olds; char *my_strtok(char *sou, char *delim) {
char *token = NULL; if(sou == NULL) { sou = olds; }
sou += strspn(sou, delim); if(*sou == '\0') { olds = sou; return NULL; }
token = sou; sou = strpbrk(token, delim);
if(sou == NULL) { olds = __rawmemchr (token, '\0'); } else { *sou = '\0'; olds = sou + 1; } return token; }
int main() { char sou[100] = " Micael_SX is so good"; char *delim = " "; char *token; token = my_strtok(sou, delim); while(token != NULL) { printf("%s\n", token); token = my_strtok(NULL, delim); } return 0; }
|
strsep函数
- 原型:
char *my_strsep(char **stringp, const char *delim)
- 作用:将字符串分割成一个个片段
- 代码:
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
|
char *my_strsep(char **stringp, const char *delim) { char *begin, *end; begin = *stringp; if(begin == NULL) { return NULL; }
if(delim[0] == '\0' || delim[1] == '\0') { char ch = delim[0]; if(ch == '\0') { end = NULL; } else { if(*begin == ch) { end = begin; } else if(*begin == '\0') { end = NULL; } else { end = strchr(begin + 1, ch); } } } else { end = strpbrk(begin, delim); }
if(end) { *end++ = '\0'; *stringp = end; } else { *stringp = NULL; } return begin; }
int main() { char source[] = "hello, world! welcome to China!"; char delim[] = " ,!";
char *s = strdup(source); char *token; for(token = my_strsep(&s, delim); token != NULL; token = my_strsep(&s, delim)) { printf(token); printf("+"); } printf("\n"); return 0; }
|
strdel函数
这个是我面试的一个上机题目,不是库里边的,所以自己写出原型。
char strDel(char str,const char chToDel),删除str中所有的chToDel字符。
源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| char *strDel(char* str,char chToDel) { assert((str!=NULL)&& (chToDel!=NULL)); char *pRefStr1, *pRefStr2; pRefStr1=pRefStr2=str;
while(*pRefStr2++){ if(*pRefStr2!=chToDel) { *pRefStr1++=*pRefStr2; } } *pRefStr1='\0'; return str; }
|
关键点: 注意C语言程序的顺序执行,以及指针。
1 2 3 4 5 6 7 8
| #include<iostream.h> #include<assert.h> void main() { char destStr[10]="aadddfcca"; char delStr='a'; strDel(destStr,delStr); cout<<destStr<<endl;}
|
参考文章
链接:
- http://blog.csdn.net/kangroger/article/details/24383571
- http://zheng-ji.info/blog/2014/02/05/shen-ru-strtokhan-shu/
- http://blog.csdn.net/gqtcgq/article/details/48399957
相关参考
C语言str函数的源码
C语言字符串函数源码详解