Size of Needle in strstr function

Jessup Jong

This is a simple question, but, when I am using the strstr function, why does the size of the needle have to be at least one character bigger than the actual text in the needle? Is this because of the null character?

When I put the needle as needle[1], it does not work. Why is it fine to put the exact number of characters for the haystack?

#include <stdio.h>
#include <string.h>


int main(void)
{
   const char haystack[21] = "IDontUnderstand?Help!";
   const char needle[2] = "?";
   char *buffer;

   buffer = strstr(haystack, needle);

   printf("The substring is: %s\n", buffer);

   return(0);
}
Joe

In theory, both strings should have the null terminating character at the end ('\0) because the function otherwise does not know when to stop reading the string:

Take the strlen function. It reads the length of the string up to the null character and can be implemented like so:

size_t strlen(const char* str)
{
    size_t len = 0;
    for(; *str; ++str, ++len);
    return (len);
}

Note the condition is *str, which also can mean *str != 0, which shows that the for loop increments the string and length until a null character is found.

However, if a null terminator is not given (e.g. you allocated char[5] for "Hello", when it should actually be char[6] - "Hello\0"), it is pure luck if the function you use stops at the end of "hello".

To make it easier, if you are writing a character array, you could instead not include the number in the braces and allow the compiler to deduce it for you to ensure that there is a null terminating character:

const char haystack[] = "IDontUnderstand?Help!";
const char needle[] = "?";

Note that you cannot put const char haystack[strlen("IDontUnderstand?Help!") + 1] = "IDontUnderstand?Help!"; as strlen("IDontUnderstand?Help!") + 1 is not a constant value.

However, if you want to dynamically allocate the memory, you can use the strlen to help:

char* get_str_buffer(const char* value)
{
    char* buf = malloc(strlen(value) + 1);
    strcpy(buf, value);
    return (buf);
}

int main(void)
{
    const char *haystack = get_str_buffer("IDontUnderstand?Help!");
    const char *needle = get_str_buffer("?");
    char *buffer;

    buffer = strstr(haystack, needle);
    printf("The substring is: %s\n", buffer);


    free(haystack);
    free(needle);
    return(0);
}

Collected from the Internet

Please contact debug[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Search text using strstr function in C

From Dev

output of function with unknowned size

From Dev

If either needle in haystack

From Dev

size of a function in memory

From Dev

Size of returned array from function

From Dev

strstr() function like, that ignores upper or lower case

From Dev

Recursive strstr function

From Dev

constexpr function as array size

From Dev

strstr and StrStrI in MinGW and with function as parameter in C

From Dev

strstr for OR searching?

From Dev

What is the problem with this haystack needle function

From Dev

What is the Time Complexity, Space complexity and Algorithm for strstr() function in C++?

From Dev

strstr function not returning NULL

From Dev

What is the problem with this haystack needle function

From Dev

Needle in a haystack jQuery

From Dev

Search text using strstr function in C

From Dev

Using strstr() function with pointers

From Dev

Size definition of strcat() function

From Dev

Size of class with virtual function

From Dev

Using the C++ strstr function to remove the part of the substring your are searching for

From Dev

PHP Regular Expression to Match Function Name and Parameters with string like Needle(needle|needle)

From Dev

Converting TCHAR to a char* for strstr function

From Dev

If either needle in haystack

From Dev

strstr() function get the position

From Dev

strstr() function returns the address 0x0000

From Dev

Subtlety in strstr?

From Dev

strstr() function in C programming

From Dev

Using self made strstr() function to check an embedded string in some other string and then replacing it by some other string (input by user)

From Dev

What if the 'needle' parameter of strpos() function contains a converted integer value that can not be applied as the ordinal value of any character?

Related Related

  1. 1

    Search text using strstr function in C

  2. 2

    output of function with unknowned size

  3. 3

    If either needle in haystack

  4. 4

    size of a function in memory

  5. 5

    Size of returned array from function

  6. 6

    strstr() function like, that ignores upper or lower case

  7. 7

    Recursive strstr function

  8. 8

    constexpr function as array size

  9. 9

    strstr and StrStrI in MinGW and with function as parameter in C

  10. 10

    strstr for OR searching?

  11. 11

    What is the problem with this haystack needle function

  12. 12

    What is the Time Complexity, Space complexity and Algorithm for strstr() function in C++?

  13. 13

    strstr function not returning NULL

  14. 14

    What is the problem with this haystack needle function

  15. 15

    Needle in a haystack jQuery

  16. 16

    Search text using strstr function in C

  17. 17

    Using strstr() function with pointers

  18. 18

    Size definition of strcat() function

  19. 19

    Size of class with virtual function

  20. 20

    Using the C++ strstr function to remove the part of the substring your are searching for

  21. 21

    PHP Regular Expression to Match Function Name and Parameters with string like Needle(needle|needle)

  22. 22

    Converting TCHAR to a char* for strstr function

  23. 23

    If either needle in haystack

  24. 24

    strstr() function get the position

  25. 25

    strstr() function returns the address 0x0000

  26. 26

    Subtlety in strstr?

  27. 27

    strstr() function in C programming

  28. 28

    Using self made strstr() function to check an embedded string in some other string and then replacing it by some other string (input by user)

  29. 29

    What if the 'needle' parameter of strpos() function contains a converted integer value that can not be applied as the ordinal value of any character?

HotTag

Archive