您的位置:首页 > 其它

C学习笔记(十)字符串 自己实现字符串函数

2016-07-19 23:33 399 查看
字符串

一.字符串就是首字符的地址

 

二.字符串函数

自己实现字符串函数

 

字符串长度函数strlen

#include <string.h>

size_t strlen(const char *s);

 #include<stdio.h>
#define MAX_SIZE 100

int my_strlen(char *src)
{
int len = 0;

while( *src != '\0')
{
len++;
src++;
}

return len;
}

int main()
{
char src[MAX_SIZE];

printf("input the src string:");
scanf("%s",&src);

printf("the src string:%s\n",src);

int result = my_strlen(src);

printf("the dst string:%d",result);

return 0;
}

 

字符串比较函数strcmp strncmp

#include <string.h>

 

       int strcmp(const char *s1, const char *s2);

 

       int strncmp(const char*s1, const char *s2, size_t n);

#include<stdio.h>
#define MAX_SIZE 100

int my_strcmp(char *src1,char *src2)
{
while(*src1 != '\0' && *src2 != '\0')
{
if(*src1 > *src2)
{
return 1;
}
else if(*src1 < *src2)
{
return -1;
}
src1++;
src2++;
}

if(*src1 == '\0' && *src2 == '\0')
{
return 0;
}

if(*src1 != '\0' && *src2 == '\0')
{
return 1;
}

if(*src1 == '\0' && *src2 != '\0')
{
return -1;
}

}

int main()
{
char src1[MAX_SIZE];
char src2[MAX_SIZE];

printf("input the src1 string:");
scanf("%s",&src1);
printf("input the src2 string:");
scanf("%s",&src2);

printf("src1 string = %s\n",src1);
printf("src2 string = %s\n",src2);

int result = my_strcmp(src1,src2);

printf("return = %d",result);

putchar('\n');
return 0;
}
#include<stdio.h>
#define MAX_SIZE 100

int my_strncmp(char *src1,char *src2,int n)
{
int i;

for(i = 0; i < n; i++)
{
while(*src1 != '\0' && *src2 != '\0')
{
if(*src1 > *src2)
{
return 1;
}
else if(*src1 < *src2)
{
return -1;
}
src1++;
src2++;
}

if(*src1 == '\0' && *src2 == '\0')
{
return 0;
}

if(*src1 != '\0' && *src2 == '\0')
{
return 1;
}

if(*src1 == '\0' && *src2 != '\0')
{
return -1;
}
}

return 0;

}

int main()
{
int n;
char src1[MAX_SIZE];
char src2[MAX_SIZE];

printf("input the src1 string:");
scanf("%s",&src1);
printf("input the src2 string:");
scanf("%s",&src2);
printf("input n:");
scanf("%d",&n);

printf("src1 string = %s\n",src1);
printf("src2 string = %s\n",src2);
printf("n = %d\n",n);

int result = my_strncmp(src1,src2,n);

printf("result = %d\n", result);

return 0;
}


字符串拷贝函数strcpy strncpy

#include <string.h>

 

       char *strcpy(char*dest, const char *src);

 

       char *strncpy(char*dest, const char *src, size_t n);

#include<stdio.h>
#define MAX_SIZE 10

void my_strcpy(char *dst,char *src)
{
while(*src != '\0' )
{
*dst = *src;
src++;
dst++;
}
*dst = '\0';
}

int main()
{
char src[MAX_SIZE];
char dst[MAX_SIZE];

printf("input the src string:");
scanf("%s",&src);

printf("src string = %s\n",src);

my_strcpy(dst,src);

printf("result = %s\n", dst);

return 0;
}

#include<stdio.h>
#define MAX_SIZE 10

char * my_strncpy(char *dst,char *src,int n)
{
int i;

for(i = 0; i < n; i++)
{
*dst = *src;
src++;
dst++;
}

*dst = '\0';

return dst;
}

int main()
{
int n ;

char src[MAX_SIZE] = {0};
char dst[MAX_SIZE] = {0};

printf("input the src string:");
scanf("%s",&src);

printf("input the n:");
scanf("%d",&n);

printf("src string = %s\n",src);
printf("n = %d\n",n);

my_strncpy(dst,src,n);

printf("result = %s\n", dst);

return 0;
}


字符串连接函数strcat strncat

#include <string.h>

 

       char *strcat(char*dest, const char *src);

 

       char *strncat(char*dest, const char *src, size_t n);

#include<stdio.h>
#define MAX_SIZE 10

char * my_strcat(char *dst,char *src)
{
int i;

while(*dst != '\0')
{
dst++;
}

while(*src != '\0')
{
*dst = *src;
src++;
dst++;
}

*dst = '\0';

return dst;
}

int main()
{
int n ;

char src[MAX_SIZE] = {0};
char dst[MAX_SIZE] = {0};

printf("input the src string:");
scanf("%s",&src);

printf("input the dst string:");
scanf("%s",&dst);

my_strcat(dst,src);

printf("result = %s\n", dst);

return 0;
}


 
#include<stdio.h>
#define MAX_SIZE 100

char *my_strncat(char *dst, char *src,int len)
{
int i;
char *temp = dst;

while(*temp != '\0')
{
temp++;
}

for(i = 0; i < len; i++)
{
*temp = *src;
src++;
temp++;
}

*temp = '\0';

temp = dst;

return temp;
}

int main()
{
char src[MAX_SIZE] = {0};
char dst[MAX_SIZE] = {0};

printf("input the src string:");
scanf("%s",&src);

printf("input the dst string:");
scanf("%s",&dst);

char *temp = my_strncat(dst,src,3);

printf("dst = %s\n",temp);

return 0;
}

字符串清空函数memset bzero

#include <string.h>

 

       void *memset(void *s,int c, size_t n);

#include <strings.h>

 

       void bzero(void *s,size_t n);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: