Posts

Showing posts with the label C Strings

C Strlen Example

Defined in header <string.h> size_t strlen ( const char * str ) ; (1) size_t strnlen_s ( const char * str , size_t strsz ) ; (2) (since C11) 1) Returns the length of the given null-terminated byte string, that is, the number of characters in a character array whose first element is pointed to by str up to and not including the first null character. The behavior is undefined if str is not a pointer to a null-terminated byte string. 2) Same as (1) , except that the function returns zero if str is a null pointer and returns strsz if the null character was not found in the first strsz bytes of str . The behavior is undefined if both str points to a character array which lacks the null character and the size of that character array < strsz ; in other words, an erroneous value of strsz does not expose the impending buffer overflow. As with all bounds-checked functions, strnlen_s is only guaranteed to be available if __S...

C Strstr Example

Defined in header <string.h> char * strstr ( const char * str , const char * substr ) ; Finds the first occurrence of the null-terminated byte string pointed to by substr in the null-terminated byte string pointed to by str . The terminating null characters are not compared. The behavior is undefined if either str or substr is not a pointer to a null-terminated byte string. Parameters str - pointer to the null-terminated byte string to examine substr - pointer to the null-terminated byte string to search for Return value Pointer to the first character of the found substring in str , or NULL if no such substring is found. If substr points to an empty string, str is returned. Example # include <string.h> # include <stdio.h> void find_str ( char const * str , char const * substr ) { char * pos = strstr ( str , substr ) ; if ( pos ) { printf ( "found the string '%s...

C Strtok Example

Defined in header <string.h> (1) char * strtok ( char * str , const char * delim ) ; (until C99) char * strtok ( char * restrict str , const char * restrict delim ) ; (since C99) char * strtok_s ( char * restrict str , rsize_t * restrict strmax , const char * restrict delim , char * * restrict ptr ) ; (2) (since C11) 1) Finds the next token in a null-terminated byte string pointed to by str . The separator characters are identified by null-terminated byte string pointed to by delim . This function is designed to be called multiples times to obtain successive tokens from the same string. If str ! = NULL , the call is treated as the first call to strtok for this particular string. The function searches for the first character which is not contained in delim . If no such character was found, there are no tokens in str at all, and the function returns a null pointer. If such character was found, ...

C Tolower Example

Defined in header <ctype.h> int tolower ( int ch ) ; Converts the given character to lowercase according to the character conversion rules defined by the currently installed C locale. In the default "C" locale, the following uppercase letters ABCDEFGHIJKLMNOPQRSTUVWXYZ are replaced with respective lowercase letters abcdefghijklmnopqrstuvwxyz . Parameters ch - character to be converted. If the value of ch is not representable as unsigned char and does not equal EOF , the behavior is undefined. Return value Lowercase version of ch or unmodified ch if no lowercase version is listed in the current C locale. Example # include <stdio.h> # include <ctype.h> # include <locale.h> # include <limits.h> int main ( void ) { /* In the default locale: */ unsigned char l ; for ( unsigned char u = 0 ; u < UCHAR_MAX ; u ++ ) { l = tolower ( u ) ; if ( l != u...

Strrchr Example

Defined in header <string.h> char * strrchr ( const char * str , int ch ) ; Finds the last occurrence of ch (after conversion to char as if by (char)ch ) in the null-terminated byte string pointed to by str (each character interpreted as unsigned char ). The terminating null character is considered to be a part of the string and can be found if searching for '\0' . The behavior is undefined if str is not a pointer to a null-terminated byte string. Parameters str - pointer to the null-terminated byte string to be analyzed ch - character to search for Return value Pointer to the found character in str , or null pointer if no such character is found. Example # include <string.h> # include <stdio.h> int main ( void ) { char szSomeFileName [ ] = "foo/bar/foobar.txt" ; char * pLastSlash = strrchr ( szSomeFileName , '/' ) ; char * pszBaseName = pLastSlash ? p...