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';
}