1

I am trying to write a function that searches for the unique part(maximum two characters) of a string in an array of strings. Although strstr and strchr are not working and crash my program for some reason. So I have resorted to trying to create something that resembles their function.

My question is this:
Why is strstr not working (something like strstr(lex[j],word)) and what am I doing wrong here?

Here is the code for the function that searches for two unique characters within an array of strings :

void convert(char word[])
{
    int i;

    for (i = 0 ; i <= strlen(word) ; i++)
    {
        if(word[i] >= 65 && word[i] <= 90)
        {
            word[i] = word[i]+32;
        }
    }
}


int  twochar(char lex[50][50],char word[], int size,char temp[3])
{
    int i,j,k,count,totlen;
    convert(word);

    for (i = 0 ; i < strlen(word) - 1 ; i++)
    {
        count = 0;
        totlen = 0;
        for(j = 0; j<size; j++)
        {
            convert(lex[j]);
            totlen += strlen(lex[j]) - 1;
            for(k = 0 ; k < strlen(lex[j]) - 1 ; k++)
            {
                if (word[i] != lex[j][k] || word[i+1] != lex[j][k + 1])
                {
                    count++;
                }
            }
        }
        if(count =  = totlen)
        {
            temp[0] = word[i];
            temp[1] = word[i+1];
        }
    }
}



int main(int argc, char *argv[])
{
    char lex[50][50] =  {"word1","word2","word3","word4" }, word[] = "test";
    char p[3];

    twochar(lex,word,4,p);
    printf("%c%c\n",p[0],p[1]);
    return 0;
}
11
  • 1
    This strlen(lex[j])-1 is a receipe for desaster. Imagine what happens if lex[j] is an emtpy "string" with the length of 0. Hint: Check the type strlen() returns. Commented Apr 22, 2015 at 16:55
  • I deleted my comment which said you do not terminate temp[] by using temp[2]='\0' because I noticed you never use temp or p as a string, only as a plain array. Commented Apr 22, 2015 at 16:57
  • well i know it's not the best solution, but what could be an alternative? Commented Apr 22, 2015 at 16:57
  • 1
    Run your code in a debugger, trace through it and inspect the relevant values. To learn how to do this you might like to read here: ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Apr 22, 2015 at 17:00
  • 1
    In that case the code would be clearer as if(word[i] >= 'A' && word[i] <= 'Z') Commented Apr 22, 2015 at 17:07

1 Answer 1

2

this line:

for(k=0;k<strlen(lex[j])-1;k++)

is the problem.

strlen(lex[0]) is 0
strlen(lex[0])-1 is -1 (0xFFFFFFFF in a 32 bit system)
k starts at 0 and is incremented until it is equal to 0xFFFFFFFF

of course, k exceeds the bounds of lex[0] when k = 50.

the result is undefined behaviour which leads to the seg fault event

To determine all the above, I compiled/linked the program via gcc, with the -ggdb parameter.

then I ran the program via 'gdb theprogram'

within gdb I entered
br main <-- break point set
run
c <-- continue
the program then crashed with a seg fault event
then I entered
bt  <-- back trace
the bt showed me this line: 'if(word[i]!=lex[j][k] || word[i+1]!=lex[j] [k+1])'
Then I entered
p k <-- print variable k
=6832   (which is WAY out of bounds)

then I entered
run
y
br theprogram.c:41    (the line number from above) <-- set another break epoint
c
the program stopped at line 41
p j
=0  ( this was the gdb response )
p k 
= 0
p i
= 0

a little thinking, 
stepping though that inner loop using 'n' <-- next
and playing on gdb 
indicated that the problem was in line 42
and resulted in revealing the root of the problem
Sign up to request clarification or add additional context in comments.

1 Comment

BTW: the compiler warnings can be eliminated by declaring certain of the loop variables as 'size_t' rather than 'int'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.