重写strcpy和最长的数字子串

来源:互联网 发布:贵州电信网络测速 编辑:程序博客网 时间:2024/05/22 14:50

重写strcpy时不明白返回temp,为什么不可以返回strDest,改成返回strDest时会出错,求解答???

#include "iostream"#include "string"using namespace std;char* strcpy1(char* strDest,const char* strSour){if(strDest==NULL || strSour==NULL)return NULL;char* temp=strDest;while((*strDest++=*strSour++)!='\0');return temp;//不明白这里返回temp,为什么不可以返回strDest,求解答???}int maxNumLength(char *outputstr, char *inputstr)  {      //最长数字串的长度      int length = 0;      //中间计算数字串的长度      int tempLen = 0;      //查找过程中查找到的字符的下标号      int index = 0;      //标记最长数字串的位置      int sign = 0;      while(inputstr[index] != '\0')      {          //判断是否为数字          if((inputstr[index] >= '0') && (inputstr[index] <= '9'))          {              tempLen++;          }else          {              if((tempLen != 0) && (tempLen > length))              {                  //记录目前为止最长数字串的位置与长度                  length = tempLen;                  sign = index;              }              tempLen = 0;          }          index++;      }  if(tempLen>length){length=tempLen;sign=index;memcpy(outputstr, inputstr+(sign-length), length);  outputstr[length]='\0';}else { if(length != 0)  {  memcpy(outputstr, inputstr+(sign-length), length);  outputstr[length]='\0';}  }      return length;  }  int main(){char *s1="sdfdsf";char s2[20];char* s3="abcd12345ed125ss123456789";printf("%s",strcpy1(s2,s1));cout<<endl;cout<<maxNumLength(s2,s3)<<endl;printf("%s",s2);cout<<endl;return 0;}


0 0
原创粉丝点击