关于指针和字符串的
#include<iostream>
using namespace std;
char* strcat1 (char*s,char*t)
{
char*d=s;
while(*s)
{
s++;
}
//s--
int j=0;
while(*s=*t)
{
s++;
t++;
}
return d;
}
int strend(char *s,char *t)
{
int i=0;
while(*s++)
{
i++;
}
while(*t)
{
t++;
}
while(*s--==*t--)//调试发现运行到这步时,字符串*S又指向‘i’字符去,而不是最后的空字符,求解释这I字符又哪来的
{
i--;
}
if(i==0)
return 1;
else
return 0;
}
int main()
{
int i=0;
char s[]="itle";
char *t="iloveyou";
char *k="you";
//char *l="eyou";
cout<<strend(k,t)<<endl;
//cout<<strend(l,t)<<endl;
cout<<strcat1(s,t)<<endl;
}
函数strend判断2个字符串,如果字符串S和字符串T的结尾相同就返回1,上面的实现有问题,难道说循环结束后指针位置有复原,但这不可能啊,指针T的还是空字符
[解决办法]
*s++也像后面那个循环那样,*s,s++这样分开写吧