Ways of Realizing the Subfunction Module of String Function in C Language

strlen(char* a); --------------------- String length
strcpy(char* a,char* b); ----- ---------string assignment (original or current input)
strncpy(char* a,char* b,int c);-------string assignment, take the first c ( Original or current input)
strcat(char* a, char* b); ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ The string is copied after the existing string (the original or current input )
strncat(char* a,char* b,int c);---------Copy the string behind the existing string, take the first c ones (original or current input)
strcmp(char* a, char* b); -------------- Compare the size of characters and output the result, greater than output 1, equal to output 0, less than output -1
strchr(char* a,int c) ;--------------------Find the first character to the end (use ASCII to represent the first character)
strrchr(char* a,int c);- ------------------Find the last character from the beginning to the end (use ASCII to represent the last character)
strstr;(char* a,char* b); ---- -----------Find the string and output to the end.

#include <stdio.h>    //自定义string子函数模块
#include <stdlib.h>
#include <math.h>
#include <string.h>
//strlen
char len(char* a)
{
    
    
    int i;
    for(i=0;;i++)
        if(a[i]==0) break;   //==0  or  =='\0'
    return i;
}

//strcpy
char* cpy(char* a,char* b)
{
    
    
    int i;
    //while(str[i]!=0) {dest[i]=str[i];i++;}
    //dest[i]=0;
    for(i=0;;i++)
    {
    
    
       if(b[i]!=0)    a[i]=b[i];
       else           break;    //for循环必须要有终止条件!
    }
    a[i]=0;
    return a;
}

//strncpy
char* ncpy(char* a,char* b,int c)
{
    
    
    int i;
    //while(str[i]!=0) {dest[i]=str[i];i++;}
    //dest[i]=0;
    for(i=0;;i++)
    {
    
    
       if(i!=c)    a[i]=b[i];
       else           break;    //for循环必须要有终止条件!
    }
    a[i]=0;
    return a;
}

//strcat
char* cat(char* a,char* b)
{
    
    
    int i=0,k=len(a);
    for(i=0;;i++)
    {
    
    
        if(b[i]!='\0')  a[k+i]=b[i];
        else            break;
    }
    return a;
}

//strncat
char* ncat(char* a,char* b, int c)
{
    
    
    int i=0,k=len(a);
    for(i=0;;i++)
    {
    
    
        if(b[i]!='\0')  a[k+i]=b[i];
        if(i==c-1)        break;
    }
    return a;
}

//strcmp
char* cmp(char* a,char* b)
{
    
    
    int i,k=len(a)>=len(b)?len(a):len(b);
    //printf("k=%d  ",k);
    for(i=0;i<k;i++)
    {
    
    
        //printf("a[i]=%d  b[i]=%d\n",a[i],b[i]);
             if(a[i]==b[i])  continue;
        else if(a[i]>b[i])   return  1;
        else if(a[i]<b[i])   return -1;
    }
    if(a[i]==b[i])  return 0;
    return a;
}

//strchr_1
char* chr_1(char* a,int b)   //不需要printf,直接输出
{
    
    
    int i,j=-1,k=len(a);
    for(i=0;i<k;i++)
    {
    
    
        if(a[i]==b)
        {
    
    
            //printf("第%d个开始替换\n",i);
            for(j=0;j<k-i;j++)
                printf("%c",a[i+j]);    //a[i]表示的是一个字符而不是字符串所以是%c而不是%s  切记!
        }
        if(j==k-i)   break;
    }
    return a;
}
//strchr_2
char* chr_2(char* a,int b)   //不需要printf,直接输出
{
    
    
    int i,j=-1,k=len(a);
    for(i=0;i<k;i++)
    {
    
    
        if(a[i]==b)
        {
    
    
            //printf("\n第%d个开始替换\n",i);
            for(j=0;j<k-i;j++)
                a[j]=a[j+i];
            int t;
            for(t=k-1;t>=k-i;t--)
                a[t]='\0';
        }
        if(j==k-i)   break;
    }
    return a;
}

//strrchr
//strrchr和strchr的不同之处在于前者没有(本行+16)行的if break终止
//它会直接到最后一个输入的ASCII值并开始输出到末尾。
//而且strrchr也同样可以通过字符%c直接输出,不需要for循环,具体见上二个子函数。
char* rchr_1(char* a,int b)  //法1
{
    
    
    int i,j,k=len(a);
    for(i=0;i<k;i++)
    {
    
    
        if(a[i]==b)   //如果变成if(a[i]==b)  a[i]=='\0'  else的话,那么在最后到达所需字符的ASCII码后,
        {
    
                 //仍会将后面的不等于相应ASCII码的初始化,因为没有if break终止。
            //printf("\n第%d个开始替换\n",i);
            for(j=0;j<k-i;j++)
                a[j]=a[j+i];
            int t;
            for(t=k-1;t>=k-i;t--)
                a[t]='\0';
        }
        //if(j==k-i)   break;
    }
    return a;
}

char* rchr_2(char* a,int b)  //法2,逆向求法
{
    
    
    int i,j,t,k=len(a);
    for(i=1;i<k;i++)     if(a[k-i]==b)  {
    
    t=k-i; break;} //printf("k=%d  %d  %d\n",k,t,k-i);
    if(i==k)return 0;
    for(j=0;j<i;j++)     a[j]=a[j+t];//{printf("%c",a[j+t]);}
    for(i=1;i<=t;i++)    a[k-i]='\0';
    return a;
}

char* str(char* a,char* b)
{
    
    
    int i,j,t,s,ka=len(a),kb=len(b);
    for(i=0;i<ka;i++)
    {
    
    for(j=0;j<kb && a[i+j]==b[j];j++){
    
    s=i;t=j;}if(j==kb) break;}//if(j==kb) break;}
    //if(t==kb-1)
    if(j==kb)
    //for(i=0;i<ka-kb;i++)    {printf("%c",a[s+i]);break;}  //单个字母直接输出。
    {
    
    for(i=0;i<ka;i++)       {
    
    a[i]=a[i+s];}}
    else return 0;
return a;
}

int main()            //测试上述自定义的字符串函数
{
    
    
    char a[100],b[100],c[100];
    int m;
    memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(c,0,sizeof(c));
    printf("%d %d\n",len(""),len("北京欢迎你!"));
    cpy(a,"hello");
    ncpy(b,"world!",4);
    printf(" cpy a is %s\n",a);
    printf("ncpy b is %s\n",b);
    cpy(c,"hello ");
    cat(c,"world!");
    printf(" cat c is %s\n",c);
    ncat(c,a,4);
    printf("ncat c is %s\n",c);
    memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(c,0,sizeof(c));
    cpy(a,"Abcd");cpy(b,"Abc");cpy(c,"Bac");
    printf("%d\n",cmp(a,b));
    printf("%d\n",cmp(b,c));
    printf("%d\n",cmp(c,c));
    cpy(b,"What is your name? 3.1415926535897932384626433832795028841971693993751");
    cpy(a,b);     printf("\n方法1:");  chr_1(a,97);
    cpy(a,b);     printf("\n方法2:%s", chr_2(a,97));
    cpy(a,b);     printf("\n方法1:%s",rchr_1(a,97));
    cpy(a,b);     printf("\n方法2:%s\n",rchr_2(a,97));//rchr_2(a,97); //将97换成'p'时,由于字符串中没有'p',输出Null,以上同理但未设置。
    cpy(a,b);     printf("\n查找2=%s",str(a,"name"));
    //printf("\n查找1=");   str(a,"name");
    cpy(a,b);     printf("\n%s",ncpy(c,a+5,18));
    memset(c,0,sizeof(c));cpy(c,"Bac");
    cpy(a,b);     printf("\n%s",ncat(c,a+5,13));   //自定义的cvpy以及ncat可以直接移动截断?
}

Wrote it a long time ago, and posting it now.
If you have any opinions and suggestions, please point out and comment, because I may not remember clearly.

Guess you like

Origin blog.csdn.net/m0_52215008/article/details/111446635