畅游榕城畅游学习考试资料┫▒ 大学课程 ▒┣┫▒ 高级语言设计 ▒┣C语言-编程技巧-C语言中操作字符串的一些函数源代码

1  /  1  页   1 跳转 查看:616

标题: C语言-编程技巧-C语言中操作字符串的一些函数源代码

C语言-编程技巧-C语言中操作字符串的一些函数源代码

很多人认为C语言中的难点是指针,对指针的理解直接关系到所编程序的好坏,所以,

              在这里列举了一些C编译器通常都有的标准函数的源代码,看过它们,就能对指针和字符串

              有所了解了.

              1. slen(),计算字符串长度

              int slen(const char sing)

              {

              int i=0;

              while(sing[i]) i  ;

              return i;

              }

              2. scpy(), 字符串拷贝.

              char *scpy(char *destination, const char *source)

{

while(*destinaton  =*source  );

return (destination-1);

}

3. scat(), 字符串的连接.

char *scat(char *target,const char *source)

{

char *original=target;

while(*target) target  ; // Find the end of the sing

while(*target  =*source  );

return(original);

}

4. seql(), 判断两个字符串是否相等.

int seql(char *s1,char *s2)

{

while((*s1==*s2)&&(*s1))

{

s1  ;

s2  ;

}

return((*s1==NULL)&&(*s2==NULL));

}

5. schr(), 在字符串中查找某个字符.

char *schr(const char *sing,int letter)

{

while((*sing!=letter)&(*sing))

sing  ;

return (sing);

}

6. chrcnt(), 计算某个字符在字符串中出现的次数.

int chrcnt(const char *sing,int letter)

{

int count=0;

while(*sing)

if(*sing==letter)count  ;

return count;

}

7. scmp(), 判断两个字符串是否相等.

int scmp(const char *s1,const char *s2)

{

while((*s1==*s2)&&(*s1))

{

s1  ;

s2  ;

}

if((*s1==*s2)&&(!*s1)) //Same sings

return o;

else if((*s1)&&(!*s2)) //Same but s1 longer

return -1;

else if((*s2)&&(!*s1)) //Same but s2 longer

else

return((*s1>*s2)?-1:1);

}





转载:href="http://www.fanqiang.com/a4/b2/20010416/134440.html">www.fanqiang.com








  文章来源:
电脑软件教程下载库




  • 上一篇: C语言-编程技巧-The Standard C Library for Linux:ctype.h
  • 引用
     

    回复:C语言-编程技巧-C语言中操作字符串的一些函数源代码

    不错
    引用
     
    1  /  1  页   1 跳转

    版权所有 畅游榕城  Discuz!NT  Sitemap

    Powered by Cyrcbbs.com 
    Processed in 0.21875 second(s) , 8 queries. 闽ICP备07046376号
    返顶部