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, for example, sscanf(str,"%d",&a).
  • sprintf(char *__stream, const char *__format, ...): output the contents of the __format string to __stream, for example, sprintf(str,"%d",i).
  • strlen(const char *str): return the number of characters from str[0] up to '\0'. Note that without O2 optimization, placing this operation in a loop condition has complexity Θ(N)\Theta(N).
  • strcmp(const char *str1, const char *str2): compare str1 str2 lexicographically. Return a negative value if str1 is lexicographically smaller, 0 if they are equal, and a positive value if str1 is lexicographically larger. Note that the return value is not necessarily one of only 0, 1, and -1; on different platforms, it follows the sign convention but need not be exactly 0, 1, or -1.
  • strcpy(char *str, const char *src): copy the characters in src to str. Both str and src are pointers to the first element of character arrays. The return value is str, including the null terminator '\0'.
  • strncpy(char *str, const char *src, int cnt): copy at most cnt characters to str. If src ends before cnt characters have been copied, write null characters to str until a total of cnt characters have been written.
  • strcat(char *str1, const char *str2): append str2 to the end of str1, replacing the final '\0' of str1 with *str2, and return str1.
  • strstr(char *str1, const char *str2): if str2 is a substring of str1, return the address of its first occurrence in str1; if str2 is not a substring of str1, then str2 is not present and the function returns NULL.
  • strchr(const char *str, int c): find the first occurrence of character c in string str and return its address. If the character is not found, return NULL.
  • strrchr(const char *str, int c): find the last occurrence of character c in string str and return its address. If the character is not found, return NULL.

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

  • The overloaded assignment operator + can concatenate two variables when + is on both sides and the operands have types string/char/char[]/const char*, returning the concatenated string (string).
  • The right-hand side of the assignment operator = can be const string/string/const char*/char*.
  • The access operator [cur] returns a reference to position cur.
  • The access functions data()/c_str() return a const char* pointer whose contents are the same as the string.
  • The capacity function size() returns the number of characters in the string.
  • find(ch, start = 0) searches for and returns the position of character ch starting from start; rfind(ch) searches backward from the end and returns the position of the first found character ch (both positions start at 0). If the character is not found, return -1.
  • substr(start, len) extracts a string of length len from start (starting at 0) in the string (if len is omitted, extract to the end of the string).
  • append(s) adds s to the end of the string.
  • append(s, pos, n) appends the n characters of string s starting at pos to the end of the current string.
  • replace(pos, n, s) deletes n characters starting at pos, then inserts string s at pos.
  • erase(pos, n) deletes n characters starting at pos.
  • insert(pos, s) inserts string s at position pos.
  • std::string overloads the comparison and logical operators; their complexity is Θ(N)\Theta(N).

This content comes from OI Wiki: Standard Library. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.