阅读背景:

c语言常见笔试编程题strcpy和strstr_cyujust的专栏

来源:互联网 

strcpy:

#include <stdio.h>
char *my_strcpy(char *dest,const char *src)
{
    int i=0;
    while(*(src+i)!='

strcpy:

#include <stdio.h>
char *my_strcpy(char *dest,const char *src)
{
    int i=0;
    while(*(src+i)!='\0')
    {
        *(dest+i)=*(src+i);
	i++;
    }
    *(dest+i) = 0;
}
int main()
{
    char a[ ]="hello";
    char b[10];
    my_strcpy(b,a);
    printf("b:%s\n",b);
    return 0;
}


strstr:

#include <stdio.h>
char *strstr(const char *haystack,const char *needle)
{
     int i=0;
     int k=0;
     int temp=0;
     while(*(haystack+i)!='\0')
     {
          if(*(haystack+i)==*(needle+k))
	  {
	      if(temp==0)
	      {
	          temp = i;
	      }
	      k++;
	  }
	  else
	  {
	      if(*(needle+k)=='\0')
	      {
	          printf("temp:%d\n",temp);
	           return haystack+temp;
	      }
	      k = 0;
	      temp = 0;
	  }
	  i++;
     }
     return  0;
}
int main()
{
     char a[]="hello world";
     char b[]="wor";
     char *p=NULL;
    
     p = strstr(a,b);
     printf("*p:%s\n",p);
     return 0;
}


 


') { *(dest+i)=*(src+i); i++; } *(dest+i) = 0; } int main() { char a[ ]="hello"; char b[10]; my_strcpy(b,a); printf("b:%s\n",b); return 0; }#include <stdio.h> char *my_strcpy(ch



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: