Thread: strstr() returns null

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    7

    strstr() returns null

    Title says it all, every time i run strstr() between the first string and the string to be searched for, I keep getting "null" returned. Code snippet below:
    Code:
        char sequence1[200];
        char start[] = "AUG";
        char *startcodon;
    
    
    /*sequence1 is already read in at this point*/
    
        startcodon = strstr(sequence1, start);
        printf("startcodon: %s\n", startcodon);
        position = startcodon - sequence1;
        printf("Position of start codon sequence 1: %d\n", position);
    It's part of a homework assignment to accept any RNA sequence (characters A,U,G,C), search for "AUG," and to print/remember its position in the string. And yes, I did check the forum guidelines on asking for homework help.
    Last edited by Jedd Lim; 09-04-2015 at 06:47 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I did a simulation, and it appears that you are mistaken:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char sequence1[200];
        char start[] = "AUG";
        char *startcodon;
    
        /* simulate reading into sequence1 */
        int position;
        strcpy(sequence1, "CCCAUGCCC");
        /* end simulation */
    
        /*sequence1 is already read in at this point*/
    
        startcodon = strstr(sequence1, start);
        printf("startcodon: %s\n", startcodon);
        position = startcodon - sequence1;
        printf("Position of start codon sequence 1: %d\n", position);
    
        return 0;
    }
    Compiling and running the above program, I get the output:
    Code:
    startcodon: AUGCCC
    Position of start codon sequence 1: 3
    So, the problem likely lies with code that you did not show, e.g., the code that reads into sequence1.
    Last edited by laserlight; 09-04-2015 at 07:02 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    7
    Hmm okay, thanks, I didn't think that that could have caused the issue. If I'm writing this portion right, would it not cause any issue issue since it's set to write to the string until the '\n' character is entered then add the '\0' termination character?
    Code:
        printf("Enter RNA sequence one:\n");
        while (position1 < 198 && letter1 != '\n')
        {
            letter1 = getchar();
            if(toupper(letter1) == 'A')
            {
                sequence1[position1] = letter1;
                position1 = position1 + 1;
            }
            else if(toupper(letter1) == 'C')
            {
                sequence1[position1] = letter1;
                position1 = position1 + 1;
            }
            else if(toupper(letter1) == 'G')
            {
                sequence1[position1] = letter1;
                position1 = position1 + 1;
            }
            else if(toupper(letter1) == 'U')
            {
                sequence1[position1] = letter1;
                position1 = position1 + 1;
            }
        }
        sequence1[position1] = '\0'; /*termination of string*/
    (*yeah, it's a bit bulky, reused it from a previous sequence reader)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should check for EOF too for terminating the loop. Furthermore, you should avoid magic numbers and unnecessary repetition. For example:
    Code:
    #include SEQUENCE_SIZE 200
    
    /* ... */
    
    char sequence1[SEQUENCE_SIZE];
    int position1;
    
    printf("Enter RNA sequence one:\n");
    while (position1 < SEQUENCE_SIZE - 1 && (letter1 = getchar()) != EOF && letter1 != '\n')
    {
        letter1 = toupper(letter1);
        if (letter1 == 'A' || letter1 == 'C' || letter1 == 'G' || letter1 == 'U')
        {
            sequence1[position1++] = letter1;
        }
    }
    sequence1[position1] = '\0'; /*termination of string*/
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2015
    Posts
    7
    Ohh ok thank you for the help.
    The code works properly now, I just am unsure of exactly why the EOF mattered. (I'm relatively new to programming in general, just the basics are down).

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    @laserlight

    Your position1 start with garbage value. It should be initialize with zero.
    (but i think you know that ;-)
    Other have classes, we are class

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, that is obvious. It was given an initial value of 0, but unfortunately the change appears to have been omitted after editing, copying and pasting. Since Jedd Lim apparently fixed it, I saw no reason to edit my example code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets always returns null
    By ido in forum C Programming
    Replies: 0
    Last Post: 04-25-2015, 06:29 AM
  2. strstr char in buffer returning null
    By lantzvillian in forum C Programming
    Replies: 2
    Last Post: 11-22-2011, 08:15 PM
  3. NULL returns every time?
    By yildizabdullah0 in forum C Programming
    Replies: 2
    Last Post: 05-26-2011, 04:29 AM
  4. mysql_use_result() returns NULL
    By mirekgn in forum C Programming
    Replies: 5
    Last Post: 03-28-2010, 01:05 PM
  5. Function returns null value instead of zero
    By drdepoy in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 03:51 PM