您的位置:首页 > 编程语言

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

2007-06-05 21:46 357 查看
/ 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐