DAY7C语言基础

strlen 函数封装

#include "stdio.h"

int my_strlen(char str[]);

int main(int argc, const char *argv[])
{
    char str[32]="qqwertyuio";
    int count=0;
    count=my_strlen(str);
    printf("count=%d\n",count);
    return 0;
}

int my_strlen(char str[]){
    int count=0;
    int i=0;
    while(str[i++]!='\0'){
        count=i;
    }
    return count;


}                                            

strcpy 函数封装

#include "stdio.h"

void my_strcpy(char str[],char str1[]);

int main(int argc, const char *argv[])
{   
    char str[32]="qwwertyuiop";
    char str1[32]="";
    my_strcpy(str,str1);                 
    printf("%s\n",str1);
    return 0;
}


void my_strcpy(char str[],char str1[]){
        int i=0;
    while(str[i]!='\0'){
        str1[i]=str[i];
        i++;
    }
    str1[i]='\0';
}

 strcmp 函数封装

 

#include "stdio.h"

void strcmp_func(char str[],char str1[]);

int main(int argc, const char *argv[])
{
    char str[32]="fwerybvf";
    char str1[32]="fwerybvf";
    strcmp_func(str,str1);
    return 0;
}

void strcmp_func(char str[],char str1[]){
    int i=0;
    int count=0;
    while(str[i]!='\0'){                                          
        count=str[i]-str1[i];
        if(count==0){
            i++;
        }
        else
            break;
    }
    if(str[i]==str1[i]){
        count=0;
    }
    else
        count=str[i]-str1[i];
    printf("%d\n",count);


    for(int i=0;str[i]!='\0'||str1[i]!='\0';i++){
        count=str[i]-str1[i];
        if(count!=0){
            break;
        }
    }
    printf("count=%d\n",count);
}

strcat函数封装

 

main.c
#include "stdio.h"
#include "my_strcat.h"


int main(int argc, const char *argv[])
{
    char str[32]="qwwear";
    char str1[32]="gfgh";
    my_strcat(str,str1);
    printf("%s\n",str);
    return 0;
}
                                        
my_strcat.h
#ifndef _MY_STRCAT_H                         
  2 #define _MY_STRCAT_H                         
  3                                              
  4 void my_strcat(char str[],char str1[]);      
  5                                              
  6                                                
  7 #endif 
my_strcat.c
void my_strcat(char str[],char str1[]){     
    int i=0;                                
    int j=0;                                
    while(str[i]!='\0'){                    
        i++;                                
    }                                       
    while(str1[j]!='\0'){                   
        str[j+i]=str1[j];                   
        j++;                                
    }                                       
    str[i+j]='\0';                          
                                            
                                            
}                                           

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值