字符串子串翻转
#include <stdio.h>
#include <iostream.h>
void reverse(char* dst,const char* src){
const char* p=src+strlen(src)-1;
char* q=dst;
char* d=q;
while(q < dst+strlen(src))
{
*q=*p;
--p;
++q;
}
*q='\0';
q=dst;
while(*q!='\0'){
while(*d!=' ')
{++d;}
--d;
char *s,*r;
for(s=q,r=d;s<r;s++,r--)
{
*s = *s + *r;
*r = *s - *r;
*s = *s - *r;
}
d=d+2;q=d;
}
}
int main(){
const char* src="i love tian an men";
char* dst=new char[strlen(src)+1];
reverse(dst,src);
cout<< dst <<endl;
delete []dst;
dst=NULL;
}
core dump了
------解决方案--------------------
while(*d!=' ')
{++d;}
前面加
if(*d!='\0')
break;