c编程中关于字符串的函数编写

来源:互联网 发布:h5简单的js小游戏 编辑:程序博客网 时间:2024/05/16 00:39

/ c.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>

//对字符串进行操作的时候,要注意是否越界!这里没有检查
unsigned int strlen(const char *source)
{ //the strlen doesn't include the "/0"
 unsigned int i=0;
 while(source[i])
  i++;
 return i;
}


char *strcpy(char *destination,const char *source)
{ //copy source string to destination string
 char *original=destination;
 while (*destination++ = *source++ )
  ;  //important void sentence
 return original;
}

char *strcat(char *target,const char *source)
{
 char *original=target;
 while (*target++)
  ;
 target--; //pay attention to the targer point the position of the string!
 while (*target++ = *source++)
  ;
 return original;
}

//测试函数
int _tmain(int argc, _TCHAR* argv[])
{

 char str1[100]="/0";
 char str2[]="hello,string!";
 
 printf("the length of the str1 is %d /n",strlen(str1));

 printf("the length of the str1 is %d /n",strlen(str2));

 printf("the str1 which  copyed from str2 is  %s /n",strcpy(str1,str2));

 printf("str1+str2 is %s /n",strcat(str1,str2));
 

 getchar();//暂停程序

 return 0;
}