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;
}
字符串拷贝函数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;
}
字符串连接函数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;
}
字符串清空函数memset bzero
#include <string.h>
void *memset(void *s,int c, size_t n);
#include <strings.h>
void bzero(void *s,size_t n);
一.字符串就是首字符的地址
二.字符串函数
自己实现字符串函数
字符串长度函数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);
相关文章推荐
- 错误与异常
- iOS9 UniversalLink使用
- 云服务器 ECS Linux IO 占用高问题排查方法
- iOS学习-资料来源
- 方法控制流程
- 【一天一道LeetCode】#299. Bulls and Cows
- 创建动态链接库,使用自己的dll中的排序算法,用Qt将排序用时在界面上用表格的形式呈现出来
- js笔记
- 经典排序算法(一)冒泡排序
- Reactivecocoa中的宏研究1
- 枚举
- Java从文件中跳过n个字节读取数据
- shell脚本练习(二)
- Struts中ActionContext和ServletActionContext的比较
- 【一天一道LeetCode】#374. Guess Number Higher or Lower
- 架构师与网红
- 【一天一道LeetCode】#374. Guess Number Higher or Lower
- 【一天一道LeetCode】#299. Bulls and Cows
- 对象的初始化和回收
- HDU 5733 tetrahedron(三维几何)