C-style strings

Objective

  • Create and use C-style strings

C-style string basics

  • C-style strings are an array of characters
  • There must be room in the array for one more character (the null character) at the end of the string to indicate the end
  • The null character is specified by the sequence: '\0'
  • There is a library with functions that support C-style strings: #include <cstring>
  • No "using" statement is used since this is an old C library
  • Declaring a C-style string that can hold up to 199 characters: char buf[200];
  • Pointers are often used to refer to C-style strings: const char* msg = "Hello, world!";
  • You can access individual characters in a C-style string using array notation: buf[0] = 'h';
  • C++ string objects can use the .c_str() function to create an equivalent C-style string

C-style string resources

  • The C library for characters is very useful with C-style strings: #include <cctype>
  • isalnum(ch) returns true iff ch is a letter or a digit
  • No "using" statement is used since this is an old C library
  • isalpha(ch) returns true iff ch is a letter
  • isblank(ch) returns true iff ch is a tab or a space
  • iscntrl(ch) returns true iff ch is a control character
  • isdigit(ch) returns true iff ch is a decimal digit
  • isgraph(ch) returns true iff ch has a graphical representation (which is the same as printable, but excludes the space character)
  • islower(ch) returns true iff ch is a lower case letter
  • isprint(ch) returns true iff ch is printable
  • ispunct(ch) returns true iff ch is a punctuation character
  • isspace(ch) returns true iff ch is a white-space character letter (such as tab, form feed, newline, space, etc.)
  • isupper(ch) returns true iff ch is an upper case letter
  • isxdigit(ch) returns true iff ch is a hexadecimal digit
  • tolower(ch) returns ch, but as a lower case version if it was upper case
  • toupper(ch) returns ch, but as an upper case version if it was lower case

C-style string functions

Assume for the following that str1 and str2 are C-style strings (pointers to char arrays), ch is a char, and n in an int:

  • strlen(str1) returns the length of str1
  • strcpy(str1, str2) copies str2 to str1 [dangerous due to possible overflow]
  • strncpy(str1, str2, n) copies the first n characters from str2 to str1 and pads str1 with 0s if str2 is shorter than n characters [less dangerous than strcpy, but no terminating null is added to str1 if str2 if n or more characters long]
  • strcat(str1, str2) concatenates str2 to the end of str1 [dangerous due to possible overflow]
  • strncat(str1, str2, n) concatenates the first n characters of str2 to the end of str1 [dangerous due to possible overflow]
  • strcmp(str1, str2) returns an int less than 0 if str1 is less than str2, greater than 0 if str1 is greater than str2, or 0 if str1 equals str2
  • strncmp(str1, str2, n) does the same as strcmp, but only compares up to a maximum of n characters
  • strchr(str1, ch) returns a pointer to the first occurrence of ch in str1, or a null pointer if it is not found
  • strpbrk(str1, str2) returns a pointer to the first occurrence of any character in str2 within str1, or a null pointer if none is found
  • strstr(str1, str2) returns a pointer to the first occurrence of str2 in str1, or a null pointer if it is not found
  • strspn(str1, str2) returns the length of characters at the start of str1 which consist of only characters continaed in str2
  • strtok(str1, str2) is used to tokenize str1 based on the delimiter list in str2 - see documentation for details of use