Thread: Unable to display the strstr result if the sub-string is at the end of the string....

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    10

    Angry Unable to display the strstr result if the sub-string is at the end of the string....

    I'm stuck again, why am I unable to display the last character of the sub-string when the sub-string is located at the end of the string? I thought a NULL character is automatically appended to the array input ?

    Also it seems that the search result won't capture the puntuation in the search. A search of sub-string "Hello!" in the string "Hello! World!" will only return 'Hell' (What an irony!!)

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    void searchstr (char str[]);
    
    main()
    {
      char str[100];
      printf ("Enter the String :");
      fgets (str, sizeof str, stdin);
      searchstr (str);
      return 0;
    }
    
    void searchstr (char str[])
    {
      char substr[100];
    
      printf ("Enter the string you want to find: ");
      fgets (substr, sizeof str, stdin);
    
      if (strstr(str, substr))
      printf ("the substring '%s' is found !!!", substr);
    }
    Is there anyway to achieve your guys level... any tips...
    And also curious to find out the market protienal for C programming... Is it still in demand in the market ?
    It's the flaws that makes the perfect more perfect....

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well, you didn't checked to see if you have \n at the end of your strings, sometimes (when possible ) fgets() add's \n at the end of the string, you should check if you have one like that and then remove.
    Code:
    char *p;
    p = strchr(str,'\n');
    if(p) *p = '\0';

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    10

    Unhappy How about the punchuation ??? It will eat the words away!!!

    But but.... A search of sub-string "Hello!" (includind the !) in the string "Hello! World!" will only return 'Hell' and the Hello is at the begning of the string, or is it the strstr function can only search for character and not the puntuation?? But, then again... if the punchutation is in the middle of the sub-string, it will still able to display without eating up the word..... I tried before....
    It's the flaws that makes the perfect more perfect....

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    void searchstr (char str[])
    {
      char substr[100];
      char *p;
      printf ("Enter the string you want to find: ");
      fgets (substr, sizeof substr, stdin);
      p = strchr(substr,'\n');
      if(p) *p = '\0';
      if (strstr(str, substr))
        printf ("the substring '%s' is found !!!", substr);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    10

    Unhappy still got problem.. help....

    Why is it when I run the sub function programe is prefrectly fine but when I impliment it to the main programe it simply skip the input part... the fgets command....

    here is the programe:

    Code:
    main()
    {
        char str[100];
        int ch;
    
        printf ("Enter the String :");
        fgets (str, sizeof str, stdin);
    
        printf ("\n");
        printf ("1/ Count Number of Characters In String.\n");
        printf ("2/ Count Number of Words In String.\n");
    .......
    
    void looksub (char str[])
    {
        char substr[100];
        char *p;
    
        printf ("Enter the substring you want to find: ");
        fgets (substr, sizeof substr, stdin);
        p = strchr(substr, '\n');
        if(p) *p = '\0';
    
        if (strstr(str, substr))
        printf ("The substring '%s' is found !!!", substr);
        else
        printf ("The substring '%s' is not found!!!", substr);
    }
    When come to the looksub function, it straight away go to the printf statement, without allowing me to key in my option.. Why !!!

    Here is an attachment of my file....
    It's the flaws that makes the perfect more perfect....

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Code:
    void looksub (char str[])
    {
        char substr[100];
        char *p;
        while(getchar() != '\n');
    
        printf ("Enter the substring you want to find: ");
        fgets (substr, sizeof substr, stdin);
        p = strchr(substr, '\n');
        if(p) *p = '\0';
    
        if (strstr(str, substr))
        printf ("The substring '%s' is found !!!", substr);
        else
        printf ("The substring '%s' is not found!!!", substr);
    }
    Ahh, and the function that is in your code, isn't the same here in the forum, check out.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    10

    Question

    yeah I know.. I can run the function... but when I impliment into my programe with many other functions together (as the file attached there)... that's when the problem come... I don't understand....
    It's the flaws that makes the perfect more perfect....

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well, I compiled your code, and it works pretty good.

    I edited a line of code there, try compiling the version I Uploaded.

    Here's the output:
    Code:
    [itzick@localhost Desktop]$ ./ass2
    Enter the String :Just testing the program
    
    1/ Count Number of Characters In String.
    2/ Count Number of Words In String.
    3/ Remove Spaces In String.
    4/ Reverse String.
    5/ Reverse Words In String.
    6/ Look For Substring In String.
    7/ Insert Substring Into String.
    
    Enter Choice: 6
    Enter the substring you want to find: ting
    The substring 'ting' is found !!!
    Last edited by Vber; 03-19-2003 at 02:01 PM.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    10

    Genius !!!! Really thanks a lot...

    Really thanks for your help... my life saviour... *kiss*

    But how come with that extra line it make so much different ???

    Code:
    while(getchar() != '\n');
    and when I run the programe, the one previosly on the forum, I can run it without that line...
    It's the flaws that makes the perfect more perfect....

  10. #10
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Read a little bit about flush the input buffer .

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Why is it when I run the sub function programe is prefrectly fine but when I impliment it to the main programe it simply skip the input part
    Code:
        ch = getchar();
        
        switch ( ch )
        {
        /* ... */
        case '6':
            looksub (str);
            break;
        /* ... */
        }
    At the prompt you type '6' and enter/return. The variable ch is assigned '6', and the newline character '\n' remains in the stdin. Then you call lookup().
    Code:
    void looksub (char str[])
    {
        char substr[100];
    
        printf ("Enter the substring you want to find: ");
        fgets (substr, sizeof str, stdin);
    Since there is already data waiting in the stdin, it doesn't need to wait for you to enter anything. It uses the '\n' that was left there and continues on.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Quick Ques on String Manipulation
    By ckuttruff in forum C Programming
    Replies: 8
    Last Post: 06-22-2008, 09:32 PM
  4. Replies: 8
    Last Post: 04-25-2008, 02:45 PM