Skip to content

Standard Library

C Standard Library

The C standard library operates on character arrays char[]/const char*.

See: fprintf, fscanf, Null-terminated byte strings

  • printf("%s", s): Use %s to output a string (character array).
  • scanf("%s", &s): Use %s to read a string (character array).
  • sscanf(const char *__source, const char *__format, ...): Read variables from the string __source, e.g., sscanf(str,"%d",&a).
  • sprintf(char *__stream, const char *__format, ...): Output the content from the __format string to __stream, e.g., sprintf(str,"%d",i).
  • strlen(const char *str): Returns the number of characters from str[0] until '\0'. Note: without O2 optimization, using this in a loop condition has complexity \(\Theta(N)\).
  • strcmp(const char *str1, const char *str2): Compares str1 and str2 lexicographically. Returns a negative value if str1 is lexicographically smaller, 0 if they are equal, and a positive value if str1 is lexicographically larger. Note: do not assume the return values are only 0, 1, -1; across different platforms, the return values follow the sign convention but are not necessarily 0, 1, -1.
  • strcpy(char *str, const char *src): Copies characters from src to str. str and src are pointers to the beginning of character arrays. Returns str including the null terminator '\0'.
  • strncpy(char *str, const char *src, int cnt): Copies at most cnt characters to str. If src ends before cnt characters are copied, null characters are written to str until a total of cnt characters are written.
  • strcat(char *str1, const char *str2): Appends str2 to the end of str1, replacing the '\0' at the end of str1 with *str2. Returns str1.
  • strstr(char *str1, const char *str2): If str2 is a substring of str1, returns the address of the first occurrence of str2 in str1; if str2 is not a substring, returns NULL.
  • strchr(const char *str, int c): Finds the first occurrence of character c in string str and returns its address. Returns NULL if the character is not found.
  • strrchr(const char *str, int c): Finds the last occurrence of character c in string str and returns its address. Returns NULL if the character is not found.

C++ Standard Library

The C++ standard library operates on string objects std::string while also providing compatibility with character arrays.

See: std::basic_string, std::basic_string_view

  • Overloads the addition operator +. When both sides of + are of type string/char/char[]/const char*, it concatenates the two variables and returns the resulting string (string).
  • The assignment operator = can have const string/string/const char*/char* on the right-hand side.
  • The access operator [cur] returns a reference to the character at position cur.
  • The access functions data()/c_str() return a const char* pointer with the same content as the string.
  • The capacity function size() returns the number of characters in the string.
  • find(ch, start = 0) searches and returns the position of character ch starting from start; rfind(ch) searches from the end and returns the position of the first found character ch (both indices start from 0) (returns -1 if not found).
  • substr(start, len) extracts a substring of length len starting from position start (0-indexed) (if len is omitted, it extracts to the end of the string).
  • append(s) appends s to the end of the string.
  • append(s, pos, n) appends n characters from string s, starting from position pos, to the end of the current string.
  • replace(pos, n, s) deletes n characters starting from pos, then inserts string s at pos.
  • erase(pos, n) deletes n characters starting from pos.
  • insert(pos, s) inserts string s at position pos.
  • std::string overloads comparison logical operators; the complexity is \(\Theta(N)\).