| « | November 2025 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | | 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 | | | | | | | |
| 公告 |
| 暂无公告... |
| Blog信息 |
|
blog名称: 日志总数:32 评论数量:44 留言数量:0 访问次数:180571 建立时间:2005年1月4日 |

| |
|
C语言一些字符串函数源码 文章收藏
eaglebetter 发表于 2006/8/1 9:51:07 |
| 很多人认为C语言中的难点是指针,对指针的理解直接关系到所编程序的好坏,所以, 在这里列举了一些C编译器通常都有的标准函数的源代码,看过它们,就能对指针和字符串 有所了解了. 1. strlen(),计算字符串长度 int strlen(const char string) { int i=0; while(string[i]) i++; return i; } 2. strcpy(), 字符串拷贝. char *strcpy(char *destination, const char *source) { while(*destinaton++=*source++); return (destination-1); } 3. strcat(), 字符串的连接. char *strcat(char *target,const char *source) { char *original=target; while(*target) target++; // Find the end of the string while(*target++=*source++); return(original); } 4. streql(), 判断两个字符串是否相等. int streql(char *str1,char *str2) { while((*str1==*str2)&&(*str1)) { str1++; str2++; } return((*str1==NULL)&&(*str2==NULL)); } 5. strchr(), 在字符串中查找某个字符. char *strchr(const char *string,int letter) { while((*string!=letter)&(*string)) string++; return (string); } 6. chrcnt(), 计算某个字符在字符串中出现的次数. int chrcnt(const char *string,int letter) { int count=0; while(*string) if(*string==letter)count++; return count; } 7. strcmp(), 判断两个字符串是否相等. int strcmp(const char *str1,const char *str2) { while((*str1==*str2)&&(*str1)) { str1++; str2++; } if((*str1==*str2)&&(!*str1)) //Same strings return o; else if((*str1)&&(!*str2)) //Same but str1 longer return -1; else if((*str2)&&(!*str1)) //Same but str2 longer else return((*str1>*str2)?-1:1); } |
|
|
回复:C语言一些字符串函数源码 文章收藏
666(游客)发表评论于2007/8/10 2:53:43 |
|
|
回复:C语言一些字符串函数源码 文章收藏
ddd(游客)发表评论于2006/9/18 19:50:46 |
| . strcpy(), 字符串拷贝. char *strcpy(char *destination, const char *source) { while(*destinaton++=*source++); return (destination-1); }
destination 变化了,并不是指向最处的地方. |
|
» 1 »
|