プログラミングプログラムはstrlen()関数,strcmp(),strcpy(),strcat()の機能を実現する

2038 ワード

1.strlen()     (         )

#include <stdio.h>
#include <assert.h>

int my_strlen(const char *str)
{
  int count=0;
  assert(str!=NULL);
  while(*str)
  {
    count++;
                str++;
  }
  return count;
}
int main()
{
  char *string= "abcdef  ds123";
  printf("%d
",my_strlen(string));   system("pause");   return 0; } 2.strcmp() ( ) #include <stdio.h> #include <assert.h> int my_strcmp(const char *str1, const char *str2) {   assert(str1!=NULL);   assert(str2!=NULL);   while(*str1 && *str2  && (*str1==*str2))   {     str1++;                 str2++;   }   return *str1-*str2; } int main() {   char *str1= "abcdde";   char *str2= "abcdef";   printf("%d
",my_strcmp(str1,str2));   system("pause");   return 0; } 3.strcpy() ( , ) #include <stdio.h> #include <assert.h> char *my_strcpy(char *dest,const char *scr)    //*scr *dest {   char *ret=dest;   assert(dest!=NULL);   assert(scr!=NULL);   while(*scr)   {     *dest=*scr;                 scr++;                 dest++;   }   *dest='\0';   return ret; } int main() {                  char str1[100]="I love the world" ;   // str1 , ,                  char *str2="China" ;                 printf( "%s
",my_strcpy(str1,str2));   system("pause");   return 0; } 4.strcat() ( ) #include <stdio.h> #include <assert.h> char *my_strcat(char *dest,const char *scr) {   char *ret=dest;   assert(dest!=NULL);   assert(scr!=NULL);   while(*dest)   {     dest++;   }   while(*dest=*scr)   {     scr++;                 dest++;   }   return ret; } int main() {                  char str1[100]="I have " ;                  char *str2="a dream!" ;                 printf( "%s
",my_strcat(str1,str2));   system("pause");   return 0; }

本文は“内心の音を探します”のブログから出て、転載して作者と連絡してください!