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%sto output a string (character array).scanf("%s", &s): use%sto 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__formatstring to__stream, for example,sprintf(str,"%d",i).strlen(const char *str): return the number of characters fromstr[0]up to'\0'. Note that without O2 optimization, placing this operation in a loop condition has complexity .strcmp(const char *str1, const char *str2): comparestr1 str2lexicographically. Return a negative value ifstr1is lexicographically smaller,0if they are equal, and a positive value ifstr1is lexicographically larger. Note that the return value is not necessarily one of only0,1, and-1; on different platforms, it follows the sign convention but need not be exactly0,1, or-1.strcpy(char *str, const char *src): copy the characters insrctostr. Bothstrandsrcare pointers to the first element of character arrays. The return value isstr, including the null terminator'\0'.strncpy(char *str, const char *src, int cnt): copy at mostcntcharacters tostr. Ifsrcends beforecntcharacters have been copied, write null characters tostruntil a total ofcntcharacters have been written.strcat(char *str1, const char *str2): appendstr2to the end ofstr1, replacing the final'\0'ofstr1with*str2, and returnstr1.strstr(char *str1, const char *str2): ifstr2is a substring ofstr1, return the address of its first occurrence instr1; ifstr2is not a substring ofstr1, thenstr2is not present and the function returnsNULL.strchr(const char *str, int c): find the first occurrence of charactercin stringstrand return its address. If the character is not found, returnNULL.strrchr(const char *str, int c): find the last occurrence of charactercin stringstrand return its address. If the character is not found, returnNULL.
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 typesstring/char/char[]/const char*, returning the concatenated string (string). - The right-hand side of the assignment operator
=can beconst string/string/const char*/char*. - The access operator
[cur]returns a reference to positioncur. - The access functions
data()/c_str()return aconst char*pointer whose contents are the same as thestring. - The capacity function
size()returns the number of characters in the string. find(ch, start = 0)searches for and returns the position of characterchstarting fromstart;rfind(ch)searches backward from the end and returns the position of the first found characterch(both positions start at0). If the character is not found, return-1.substr(start, len)extracts a string of lengthlenfromstart(starting at0) in the string (iflenis omitted, extract to the end of the string).append(s)addssto the end of the string.append(s, pos, n)appends thencharacters of stringsstarting atposto the end of the current string.replace(pos, n, s)deletesncharacters starting atpos, then inserts stringsatpos.erase(pos, n)deletesncharacters starting atpos.insert(pos, s)inserts stringsat positionpos.std::stringoverloads the comparison and logical operators; their complexity is .