用c++实现:字符串处理

来源:百度知道 编辑:UC知道 时间:2024/06/02 20:46:24
我是一个菜鸟级的C++初学者,有很多的东西不明白.所以到这里求救,希望可以得到高手的帮助.有高手可以告诉我怎样能实现字符串的复制 逆转和子串的截取这几点吗?希望有高手能帮助一下,在下感激不尽.

1. 字符串的复制
char *strcpy(char *target, const char *source)
{ char *t=target; while(*target++=*source++); return t;}

2. 字符串逆转
char *reverse(char *s)
{ int i=strlen(s); char ch,*t=s+i; char *temp=s;
if (i) --t;
while(t!=s) {ch=*t;*t=*s;*s=ch; --t; --s;}
return temp;
}
3.子串的截取
char *midstr(const char *source,
int start, int num, char *target)
{
char *t=target;
*target=0;
for (int i=0;i<start;i++)
if (*source==0) return t;
for (i=0;i<num; i++)
if ((*target++=*source++)==0) return t;
*target=0; return t;
}

char *
string
Cstring
你说的是哪种字符串