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;
}
//
#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;
}
相关文章推荐
- 编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个"。
- 【编程之法】如何最快的判断出短字符串b中的所有字符是否都在长字符串a中?编写函数bool StringContain(string&b,string&a)实现此功能。
- 编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC
- 关于《程序员编程宝典》中编写一个函数,作用是把一个char组成的字符串循环右移n位的问题
- C语言编程练习5——编写函数void change(char *a,char *b,char*c)。 函数功能是首先把b指向的字符串逆向存放
- 编写程序时关于字符串时的一些主要函数
- 编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”
- 编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
- 编程:编写一个截取字符串的函数,(网上流传的答案有的是错的)输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4
- 编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串
- 【编程之法】如何最快的判断出短字符串b中的所有字符是否都在长字符串a中?编写函数bool StringContain(string&b,string&a)实现此功能。
- 【C编程练习】华为2013校园招聘机试题目题4:删除一个字符串中出现次数最少的字符,函数原型为:char * delChar(char *s,int iLen)
- 关于SQL字符串处理的一些函数
- 程序员编程艺术:第四章、现场编写类似strstr/strcpy/strpbrk的函数
- 编写一个函数isMerge,判断一个字符串str是否可以由其他两个字符串part1和part2“组合”而成
- 编程菜鸟的日记-初学尝试编程-编写函数实现strlen功能(总结考察点)
- C语言 编写一个函数,它从一个字符串中提取一个子字符串
- 编写一个业务函数,实现字符串(前后各有三个空格,单词前后也均有空格) " i am student, you are teacher " , 各个单词首字符大写
- 【误区】关于Linux C 网络编程函数accept第三个参数
- 利用C实现:编写一个函数,它从一个字符串中提取一个子字符串。