How does Strdup work in C?


strdup() and strndup() Functions

The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3). The strndup() function is similar, but copies at most n bytes.

Difference between strstr() and stristr() in PHP

The stristr() is a case-insensitive function which is similar to the strstr(). Both functions are used to search a string inside another string. The only difference between them is that stristr() is case-insensitive whereas strstr() is case-sensitive function.

Additional Functions in C

  • memmove(): The memmove() function copies count bytes of src to dest. This function allows copying between objects that might overlap as if src is first copied into a temporary array.
  • strtoul(): The strtoul() function in C/C++ converts the initial part of the string in str to an unsigned long int value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.
  • strpbrk() in C: The function strpbrk() is used to find the first character of the first string and matches it to any character of the second string. It returns NULL if no matches are found otherwise, it returns a pointer to the character of the first string that matches the character of the second string.
  • strchr() in C: The strchr() function returns a pointer to the first occurrence of c that is converted to a character in string. The function returns NULL if the specified character is not found.

About str() Method

The str() method returns a string, which is considered an informal or nicely printable representation of the given object.

Lascia un commento