Thread: strstr() question

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    strstr() question

    hi, i was implementing strstr() to see how it works.

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    char str[] = "this is a test";
    char*s;
    s=strstr(str,"test");
    cout<<s<<endl;
    
    }

    i have two question .

    question 1 > what is the full form of strstr() ?

    for example, if i write strcpy ---> it means string copy.

    similarly what is the meaning of strstr() ?

    the syntaxex dont give the full form of this function. can you tell what is the literal meaning?


    question 2.


    without assigning a memory (by new keyword ) the code is running!!

    look, i have simply tested with only char *s; but no memory allocated.
    blue_gene

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) most likely string string
    2) Read up on pointers more. There is no need to assign memory in this example. s will get the value that is returned by strstr(). In the example it will get the address of the first t in test, or str[10]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what is the full form of strstr() ?
    Does it really matter? Just call it "string string" and be done with it.

    >the code is running!!
    I'm surprised:

    >#include<iostream>
    >using namespace std;
    These will give you errors in C.

    >s=strstr(str,"test");
    You didn't include <string.h> for strstr.

    >cout<<s<<endl;
    If you compile as C, the compiler will complain about this too.

    >(by new keyword )
    You mean by malloc, since you posted on the C board.

    >but no memory allocated
    Because strstr returns a pointer to a location within the string you give it as the first argument. The memory already exists, has been initialized (we hope!) and theoretically, you own it. There's no need to allocate memory.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    You didn't include <string.h> for strstr.
    hmmm, but it worked in my compiler. my compiler version

    g++ -v

    gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
    blue_gene

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but it worked in my compiler
    Do you want a cookie? It works on my compiler too (with a warning), but that doesn't make it right.
    My best code is written with the delete key.

  6. #6
    lost in the stack...
    Join Date
    Apr 2004
    Posts
    11
    cout<<s<<endl;
    Doesn't the above part of the code make it C++, and not plain old C?
    Just thought I'd point that out.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Doesn't the above part of the code make it C++, and not plain old C?
    Yes. It was already mentioned, but thank you anyway.
    My best code is written with the delete key.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's a C version of the code. Please don't post C++ code in this forum, instead, use this one.
    Code:
     #include <stdio.h>
     #include <string.h>
     
     int main(void)
     {
       char buf[] = "this is a small message";
       char *p;
       
       if ((p = strstr(buf, "small")) != NULL)
       {
     	printf ("p now points to --- %s ---\n", p);
       }
       
       return(0);
     }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >hmmm, but it worked in my compiler. my compiler version
    >g++ -v
    g++ is the GNU C++ compiler. For C, you use gcc.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    sorry for posting C++ code.

    my demo test was bad. i thought it will return only the matched string. but it is returning the whole string after the matched string.

    anyway,

    from hammers code can i simply extract "small" to be printed ?



    another code

    Code:
     #include <stdio.h>
     #include <string.h>
    
     int main(void)
     {
       char buf[] = "this is a small message";
       char *p;
    
       if ((p = strstr(buf, "is")) != NULL)   // a littele change here
       {
            printf ("p now points to %s \n", p);
       }
    
       return(0);
     }

    output
    ./a.out

    p now points to is is a small message


    look there is double "is".....why two "is" ?
    blue_gene

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Look at the message. Now lop off the first two letters. Whaddaya get?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >from hammers code can i simply extract "small" to be printed ?
    It takes a little more effort:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char buf[] = "this is a small message";
      char *p;
    
      if ((p = strstr(buf, "small")) != NULL)
      {
        p[ strlen ("small") ] = '\0';
        printf ("p now points to %s \n", p);
      }
    
      return(0);
    }
    If you don't want to modify the original string then you're stuck with copying the right number of characters to another buffer and then printing that one.
    My best code is written with the delete key.

  13. #13
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    similarly what is the meaning of strstr() ?
    I'd say it means "String string" referring to its usage where it searchs for a string within a string.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question About Finding a Word and Replacing It
    By Zildjian in forum C Programming
    Replies: 3
    Last Post: 09-23-2003, 08:50 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM