50 likes | 138 Views
Strings in C are my friend, and they can be yours, too. An inspiring and epic tale by Erik Speyer, John Sullivan, and Dane Bennington. Strings in C.
E N D
Strings in C are my friend, and they can be yours, too. An inspiring and epic tale by Erik Speyer, John Sullivan, and Dane Bennington
Strings in C • There is no ‘string’ type in C, rather, an array of characters with some useful functions available to the user to manipulate the array as if it were a string. • These functions mostly reside in the string.h header file
String.h – Manipulating Strings • stpcpy - Copy one string into another. • strcmp - Compare two strings alphabetically • strerror – Get a specific error message string • strlen – Returns the length of the string • strncat – Concatenate two strings together • strncmp – Compares n-chars in two strings • strncpy - Copy n-chars of one string into another • strcasecmp - case insensitive version of strcmp() • sprintf – Outputs a string to a buffer
String.h – Searching Strings • strchr –First occurrence of a char in the string. • strrch –Last occurrence of a char in the string. • strstr – The first occurrence of one string in another • strspn – Chars in two strings that match • strcspn- Chars in two strings that don’t match • strtok – Split a string at a specified character • strpbrk – Looks for any char between two strings.
Idiosyncrasies of Strings in C • Strings have no idea how long they are. They get around this shortcoming by using a null-character (‘\0’) that tells the subroutine it can stop looking. • Strings have a set size. Remember this if you are appending strings – make sure the final string is big enough to hold both of them. • String commands that take a part of a string make a string that is not null-terminated, so watch out. • The string tokenizer (strtok) will return the first tokenized word if you pass it a string, but all subsequent calls to find the next token in the string require you to pass it null(!).