function strstr (from the library string.h) made by me

0

I made a program that pretends to be the function strstr , of the library string.h . Enter 2 strings per keyboard and the function finds the second string in the first one by returning a pointer (if it does not find it returns NULL ) that points to the first string (where the second one is located).

I do not know how to perform the function without modifying the original string. When the function returns the "pointer" (pointer type char that returns to main ) and automatically modifies the original string, and do not want that.

I hope I have been clear, of course, thank you very much.

    #include <stdio.h>
    #include <stdlib.h>
    #define TAM 30
    char* mistrstr(const char*,const char*);
    int main()
    {
     char cadena[TAM];
     char cadena1[TAM];
     char *aparicion;
     printf("Ingrese la primer cadena:");
     gets(cadena);
     fflush(stdin);
     printf("Ingrese la segunda cadena:");
     gets(cadena1);
     fflush(stdin);
     aparicion=mistrstr(cadena,cadena1);
     printf("Primera aparicion (con mi funcion):%s\n",aparicion);
     return 0;
     }
    /////////////////////////////////////////////////////   
    char* mistrstr(const char* cadena,const char* cadena1)
    {
      int i=-1;
      char *apuntador,*cad,*cad1;
      apuntador=cadena;
      cad=(char*)cadena;
      cad1=(char*)cadena1;
      while(*cad)
   {
       if(*cad1==*cad)
         i=0;
      else
         cad++;
      while(i>=0)
      {
         *(apuntador+i)=*cad;
         i++;
         cad++;
          if(*cad=='
    #include <stdio.h>
    #include <stdlib.h>
    #define TAM 30
    char* mistrstr(const char*,const char*);
    int main()
    {
     char cadena[TAM];
     char cadena1[TAM];
     char *aparicion;
     printf("Ingrese la primer cadena:");
     gets(cadena);
     fflush(stdin);
     printf("Ingrese la segunda cadena:");
     gets(cadena1);
     fflush(stdin);
     aparicion=mistrstr(cadena,cadena1);
     printf("Primera aparicion (con mi funcion):%s\n",aparicion);
     return 0;
     }
    /////////////////////////////////////////////////////   
    char* mistrstr(const char* cadena,const char* cadena1)
    {
      int i=-1;
      char *apuntador,*cad,*cad1;
      apuntador=cadena;
      cad=(char*)cadena;
      cad1=(char*)cadena1;
      while(*cad)
   {
       if(*cad1==*cad)
         i=0;
      else
         cad++;
      while(i>=0)
      {
         *(apuntador+i)=*cad;
         i++;
         cad++;
          if(*cad=='%pre%')
        {
            *(apuntador+i)='%pre%';
            return apuntador;
        }
    }
   }
     return NULL;
    }
     ////////////////////////////////////////////////
') { *(apuntador+i)='%pre%'; return apuntador; } } } return NULL; } ////////////////////////////////////////////////
    
asked by micaela 07.09.2017 в 23:11
source

1 answer

0

A string, by definition, ends with a null character.

The strstr function returns a pointer to the position where the substring is ... but does not change the original string :

int main()
{
   const char cadena[] = "abcSubcadenadef";
   const char subcadena[] = "Subcadena";
   const char *ret = strstr(cadena,subcadena);

   printf("%s\n", ret);

   return 0;
}

This example will return:

Subcadenadef

With this in mind, the only thing the function does is to iterate over cadena and, in each iteration, verify if it finds the subcadena :

const char* strstr(const char* cadena, const char* subcadena)
{
  for( ; *cadena; ++cadena )
  {
    const char* ptr1 = subcadena;
    for( const char* ptr2 = cadena; *ptr1==*ptr2 && *ptr1; ++ptr1, ++ptr2);

    if( *ptr1 == '
int main()
{
   const char cadena[] = "abcSubcadenadef";
   const char subcadena[] = "Subcadena";
   const char *ret = strstr(cadena,subcadena);

   printf("%s\n", ret);

   return 0;
}
' ) return cadena; } return NULL; }

Now, if you want your function to return the substring in case you find it (finished with 'subcadena' ) you do not have many options:

  • Return cadena . Technically it is what you want to return and you do not need to modify anything.
  • Modify %code% . Viable and simple but you do not want to do this.
  • You create a dynamic array and copy the string there. Of course, you'll have to remember to free the memory out of the array ... it's not very practical.

For lack of specifying a little more what you expect my answer ends here. My opinion is that what you want is unnecessary and artificial (if you make a function to search for a substring what interests you is to know if it is found and where ... recovering the substring is absurd because it is something that you already have stored in the substring itself ).

    
answered by 07.09.2017 / 23:34
source