-3

I have built a function that searchs a subtring in a source string and fills an array with the index of the subtring found it.

I debug it and the array of index fills with the correct index but when i return the pointer and tried to print it, just get blank

#include <stdlib.h>
#include <string.h>

#define AUX_LENGTH 1000

char* find_sub_string(char *source,char *sub_string);

int main()
{
    char text[]="yesterday i was walking";
    char find[]="e";

    printf("%s \n",find_sub_string(text,find));

    return 0;
}

/*!
 *Function to find the index of a substring in a source string
 @param *source string source to search
 @param *sub_string substring to find
 @return result returns the indexs of the found subtring
 @return NULL in case not found or subtring bigest than source string
*/

char* find_sub_string(char *source,char *sub_string)
{
    size_t l_source=strlen(source);
    size_t l_sub_string=strlen(sub_string);

    if(l_sub_string>l_source)
        return NULL;

    char aux[AUX_LENGTH]="";
    static char result[AUX_LENGTH];

    int i,j;

    for(i=0,j=0; i<l_source;i++)
    {
        memcpy(aux,source+i,l_sub_string);
        if (memcmp(aux,sub_string,l_sub_string)==0)
        {
            result[j++]=i+1;
        }
    }
    result[j]='\0';

    if (j>0)
        return result;
    else
        return NULL;
}

Edit: Example

char text[]="yesterday i was walking";
char find[]="e";
char *p=find_sub_string(text,find);

*p must be a pointer char arra with the index of the position founded, like this: *p={"25"} two and five are the postions of "e" i the source.

Edit 2 I change the code to an array of size_t is more easy to handle without the ASCII convertion, i could use strstsr but i must to embeded in anohter function because i would like to search in all the string and not keep just with the first math.

Here the new code thanks for the comments i could improve a couple of things i will prove with strstr:

size_t* find_sub_string(char *source,char *sub_string)
{
    size_t l_source=strlen(source);
    size_t l_sub_string=strlen(sub_string);

    if(l_sub_string>l_source)
        return NULL;

    size_t *result = malloc(sizeof(size_t)*AUX_LENGTH);

    size_t i,j;

    for(i=0,j=0; i<l_source;i++)
    {
        if (memcmp(source+i,sub_string,l_sub_string)==0)
        {
            result[j++]=i+1;
        }
    }
    result[j]='\0';

    if (j>0)
        return result;
    else
        return NULL;
}

int main()
{
    char text[]="yesterday i was walking";
    char find[]="y";
    size_t *p=find_sub_string(text,find);
    printf("%lu \n",p[0]);

    return 0;
}
Edgar Gomez
  • 145
  • 1
  • 1
  • 10
  • 1
    Why are you trying to store indexes in `char` arrays? Why not `int` or `size_t`? – Ajay Brahmakshatriya Apr 15 '17 at 18:38
  • 1
    `result` consists of some integers that don't have much to do with the string. – ForceBru Apr 15 '17 at 18:39
  • Also it is generally not a good idea to return references to static arrays. Other calls to same functions may change the data. – Ajay Brahmakshatriya Apr 15 '17 at 18:41
  • One giant problem is what you are trying to printf is not really printable with %s -- either NULL or a bunch of 1-byte integers. – John Hascall Apr 15 '17 at 18:43
  • I can also see no reason why you would copy into `aux` -- you could just memcmp against `source+i` directly. – John Hascall Apr 15 '17 at 18:45
  • You will overflow `char *source` when reading, because the loop goes to its full length, where it should go to `i <= l_source - l_sub_string;`. You should also check `l_sub_string != 0`. – Weather Vane Apr 15 '17 at 18:51
  • result[j]=i+1+'0'; yeah i need to move to ASCII, and other things i should use malloc rather than stactic, there are wonderfull comments i would like to see them like ansewers to check in – Edgar Gomez Apr 15 '17 at 18:57
  • No, the comments say there is no need for `result`. The requirement is unclear: the question says "fills an array with the index of the subtring found it" which makes little sense. The standard library function `strstr` returns a pointer to the first occurrence of the substring in `char *source`. – Weather Vane Apr 15 '17 at 19:01
  • Your question is unclear. Please edit the question with example inputs and required output. – Weather Vane Apr 15 '17 at 19:10

1 Answers1

0

The idea behind a substring finding function like strstr is to return the actual position of the substring in the string. In your implementation, you actually copy the substring into the result. It almost does not worth the searching.

This implementation returns the actual position or NULL.

char *find_sub_string(char *source, char *sub_string)
{
    size_t l_source=strlen(source);
    size_t l_sub_string=strlen(sub_string);
    int i,j;

    if (l_sub_string>l_source)
        return NULL;

    for (i=0,j=0; i<l_source-l_sub_string;i++)
    {
        if (memcmp(source+i,sub_string,l_sub_string)==0)
            return source+i
    }

    return NULL;
}
eyalm
  • 3,366
  • 19
  • 21