现在的位置: 首页 > 综合 > 正文

c++面试题 之字符串相关

2013年09月21日 ⁄ 综合 ⁄ 共 2283字 ⁄ 字号 评论关闭

1.char* strcpy(char*src,char*des);//字符串拷贝

已知strcpy函数的原型是:
char * strcpy(char * strDest,const char * strSrc);
1.不调用库函数,实现strcpy函数。
2.解释为什么要返回char *。

解说:
1.strcpy的实现代码

 
  1. char * strcpy(char * strDest,const
    char * strSrc)
  2. {
  3. if ((strDest==NULL)||(strSrc==NULL))
    //[1]
  4. throw "Invalid argument(s)";
    //[2]
  5. char * strDestCopy=strDest;
    //[3]
  6. while ((*strDest++=*strSrc++)!='\0');
    //[4]
  7. return strDestCopy;
  8. }

2.int strcmp( const char *str1, const char *str2 );//比较字符串

int strcmp(const char* src, const char *dst)
{
    int rtn = 0;

    assert(src && dst); 
    while((rtn = *src - *dst) == 0)
    {     
        src++;
        dst++;
        if(*(src - 1) == '\0')
        {
            return 0;
        }        
    }

    return rtn;
}

3:char *strstr( const char *str1, const char *str2 );//功能:函数返回一个指针,它指向字符串str2 首次出现于字符串str1中的位置,如果没有找到,返回NULL。

  1. const char* StrStr(const
    char *str1, const
    char *str2)
  2. {
  3. assert(NULL != str1 && NULL != str2);
  4. while(*str1 !=
    '\0')
  5. {
  6. const char *p = str1;
  7. const char *q = str2;
  8. const char *res = NULL;
  9. if(*p == *q)
  10. {
  11. res = p;
  12. while(*p && *q && *p++ == *q++)
  13. ;
  14. if(*q == '\0')
  15. return res;
  16. }
  17. str1++;
  18. }
  19. return NULL;
  20. }

4.char* copy(char*ch,char*c1,char*c2)//字符串替换
{
 char*pc = ch;
 char*pc1 = c1;
 char*pc2 = c2;
 //计算c1 c2长度是否相等分别做相应处理
 char*pd1 = c1;
 int len_1 = 0;
 char*pd2 = c2;
 int len_2 = 0;
 for(;*pd1!='\0';pd1++,len_1++){};
 for(;*pd2!='\0';pd2++,len_2++){};

 for (;*pc!='\0';pc++)//遍历ch
 {
  if (*pc==c1[0])//如果和c1的第一个字符相同
  {
   char*ps = pc+1;
   char*ps1 = pc1+1;
   for (;*ps1!='\0';ps1++,ps++)//同步遍历ch c1;
   {      //原指针不可动,因为后面找到了还要用,所以定义临时指针
    if (*ps1!=*ps)
    {
     break;
    }
   }
   if (*ps1=='\0')//找到了
   {
    //如果c1 c2不相等

    //不相等的时候我们还要根据
    //  //两个字符串的长度关系作不同处理
    //  //把足够第3个字符串拷贝到原始字符串内的
    //  //空间给预留出来
    if (len_2 < len_1)
    {
     // //第三个字符串长度小于第二个长度
     // //向前往后移动-->替换

     char*pt1 = pc+len_1;
     char*pt2 = pc+len_2;
     for (;*pt1!='\0';pt1++,pt2++)
     {
      *pt2 = *pt1;
     }
     *pt2 = *pt1;

     /*char* tpS = pc + len_1;
     char* tpD = pc + len_2;
     for (; *tpS != '\0'; tpS++,tpD++)
     {
      *tpD = *tpS;
     }
     *tpD = *tpS;*/
    }
    else
    {
     //  //第三个字符串长度比第二个长的时候
     //  //从后往前拷贝

     char*p_s = pc;
     for (;*p_s!='\0';p_s++){}
     char*p_e = p_s+(len_2-len_1);
     for (;p_s>=pc+len_1;p_e--,p_s--)
     {
      *p_e = *p_s;
     }

    /* char* tpS = pc;
     for(; *tpS != '\0'; tpS++)
     {}
     char* tpD = tpS + (len_2 - len_1);
     for (; tpS >= pc + len_1; tpS--,tpD--)
     {
      *tpD = *tpS;
     }*/
    }
   }

   //如果c1 c2 相等:直接替换
   char*ppc1 = pc;//就从找到的位置开始替换
   char*ppc2 = c2;
   for (;*ppc2!='\0';ppc1++,ppc2++)
   {
    *ppc1=*ppc2;//右值;左位;
   }
  }
 }
 return ch;
}

 

抱歉!评论已关闭.