C语言 stringcpy,stringcat,stringcmp实现

来源:互联网 发布:数据共享交换平台 编辑:程序博客网 时间:2024/06/05 19:52
C语言 stringcpy,stringcat,stringcmp实现 - kingshow - 博客园

http://www.cnblogs.com/kingshow123/p/stringoperation.html

#include<stdio.h>#include<conio.h>//实现从源字符串string到目的字符串str的复制函数char* stringCpy(char* str,const char* string){    char* s=str;    while(*string)    {        *s++=*string++;    }    *s='\0';    //返回目的字符串的首地址    return str;}//函数将字符串string链接到字符串str的尾部char* stringCat(char* str,const char* string){    char* s=str;    //找到字符串str的尾部    while(*s)    {        s++;    }    while(*string)    {        *s++=*string++;    }    *s='\0';    //返回目的字符串的首地址    return str;}//比较大小的函数//实现两个字符串str和string的比较//如果str小于string返回负值,如果str大于string返回正直,如果str等于string返回0int stringCmp(const char* str,const char* string){    while((*str)&&(*string)&&(*str==*string))    {        str++;        string++;    }    return (int)(*str-*string);}int main(){    char s1[20];    const char* s2="abc";    const char* s3="def";    char* pc;    int cmp;    puts("**************************************");    puts("|   The program will complish:       |");    puts("|   strcpy,strcat,strcmp             |");    puts("**************************************");    printf("The string s2 is:%s\n",s2);    printf("The string s3 is:%s\n",s3);    pc=stringCpy(s1,s2);    printf("This is stringcpy s2 to s1,s1 is :\n");    puts(pc);    pc=stringCat(s1,s3);    printf("This is stringcat s1 to s3,s1 is :\n");    puts(pc);    cmp=stringCmp(s2,s3);    if(cmp==0)        printf("\nThe string s2 is equal to s3\n");    else if(cmp<0)        printf("\nThe string s2 is smaller to s3\n");    else        printf("\nThe string s2 is larger to s3\n");    getch();//从控制台读取一个字符,但不显示在屏幕上,实现在该位置暂停一下,按任意键继续    return 0;}

0 0
原创粉丝点击