#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0

    Searchstring strstr wrong output


    #include <stdio.h>
    #include <string.h>
    char tracks[][80] = {
    "I left my heart in Harvard Med School",
    "Newark, Newark - a wonderful town",
    "Dancing with a Dork",
    "From here to maternity",
    "The girl from Iwo Jima",
    };
    void find_track(char search_for[])
    {
    int i;
    for (i = 0; i < 5; i++) {
    if (strstr(tracks[i], search_for))
    printf("Track %i: '%s'\n", i, tracks[i]);
    }
    }
    int main()
    {
    char search_for[80];
    printf("Search for: ");
    fgets(search_for, 80, stdin);
    find_track(search_for);
    return 0;
    }
    itz not giving correct output
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    1. Use [code][/code] tags when posting code!

    2. fgets() stores a \n in the buffer. You may need to remove this before using the string in strstr calls (unless you're specifically trying to match a \n at the end of a line).

    Comments on this post

    • Technovicez agrees : thankz aton
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    27
    Rep Power
    0
    Exactly
    i did
    array[strlen(array)-1]='\0';

    and i got the result

    thankz a ton

IMN logo majestic logo threadwatch logo seochat tools logo