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 [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursive strstr function

From Dev

strstr function not returning NULL

From Dev

Using strstr() function with pointers

From Dev

strstr() function get the position

From Dev

strstr() function in C programming

From Dev

What is the problem with this haystack needle function

From Dev

What is the problem with this haystack needle function

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

Search text using strstr function in C

From Dev

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

From Dev

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

From Dev

Search text using strstr function in C

From Dev

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

From Dev

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

From Dev

strstr() function returns the address 0x0000

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?

From Dev

strstr for OR searching?

From Dev

Subtlety in strstr?

From Dev

constexpr function as array size

From Dev

size of a function in memory

From Dev

output of function with unknowned size

From Dev

Size definition of strcat() function

From Dev

Size of class with virtual function

From Dev

If either needle in haystack

From Dev

Needle in a haystack jQuery

From Dev

If either needle in haystack

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

Size of returned array from function

Related Related

  1. 1

    Recursive strstr function

  2. 2

    strstr function not returning NULL

  3. 3

    Using strstr() function with pointers

  4. 4

    strstr() function get the position

  5. 5

    strstr() function in C programming

  6. 6

    What is the problem with this haystack needle function

  7. 7

    What is the problem with this haystack needle function

  8. 8

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

  9. 9

    Converting TCHAR to a char* for strstr function

  10. 10

    Search text using strstr function in C

  11. 11

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

  12. 12

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

  13. 13

    Search text using strstr function in C

  14. 14

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

  15. 15

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

  16. 16

    strstr() function returns the address 0x0000

  17. 17

    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?

  18. 18

    strstr for OR searching?

  19. 19

    Subtlety in strstr?

  20. 20

    constexpr function as array size

  21. 21

    size of a function in memory

  22. 22

    output of function with unknowned size

  23. 23

    Size definition of strcat() function

  24. 24

    Size of class with virtual function

  25. 25

    If either needle in haystack

  26. 26

    Needle in a haystack jQuery

  27. 27

    If either needle in haystack

  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

    Size of returned array from function

HotTag

Archive