The following functions process character strings. Unless otherwise specified, their prototypes are located in the cstring header.
strlen( const char* str );
Returns the length of the string str, i.e., the number of characters in the string whose first character is located at the address stored in str. The null character is not counted.
cout << strlen("exam"); // 4
char s[10]="child";
cout << strlen(s); // 5
cout << strlen(s + 2); // 3
strcpy( char* dest, const char* src );
Copies the characters from the string located at the address src, including the null character, into the string whose first element is at the address dest.
The function returns the address dest.
char s[21], t[21] = "child";
strcpy(s , "exam");
cout << s; // exam
strcpy(s , t);
cout << s; // child
strcpy(s , t + 2);
cout << s; // ild
strcpy(s + 2 , t);
cout << s; // ilchild
strcat( char *dest, const char *src );
Appends (concatenates) the characters from the string located at the address src, including the null character, to the string whose first element is at the address dest.
The function returns the address dest.
char s[21]="exam", t[21] = "child";
strcat(s , t);
cout << s; // examchild
strcat(s , t + 2);
cout << s; // examchildild
strchr( char * str, char ch );
Searches for the character ch in the string whose first character is located at the memory address str.
The function returns the NULL address if the character ch does not appear in the string str, or the address of the first occurrence of ch in str if ch appears.
char s[21]="exam";
char * p = strchr(s , 'x');
cout << p; // xam
char ch = 'i';
if(strchr("aeiou" , ch) != NULL)
cout << "YES";
else
cout << "NO";
// will output YES
strstr( char * s, char * t );
Searches for the string t in the string whose first character is located at the memory address s.
The function returns the NULL address if the string t does not appear in the string s, or the address of the first occurrence of t in s if t appears.
char s[21]="baccalaureate";
char * p = strstr(s , "cal");
cout << p; // calaureate
strcmp( char * s, char * t );
Lexicographically compares the two character strings:
char s[21]="steam", t[21]="primary";
if(strcmp(s , t) < 0)
cout << "Yes"
else
cout << "No";
// will output No; the word "steam" is lexicographically after "primary"
strtok( char *str, const char *sep );
The strtok function extracts from a character string one substring (word) delimited by characters from the sep string. The function is called in two ways:
The result of strtok is the starting address of the current substring extracted, or NULL if no more substring can be extracted from the given string.
The sequence below extracts words (separated by characters from the set {' ', ',', '.'}) from a string s and prints them on separate lines. Assume the string s is declared and read.
char sep[]=" .,";
char * p = strtok(s , sep);
while(p != NULL)
{
cout << p << endl;
p = strtok(NULL , sep);
}
In this section you can generate a summary of the page content using AI! Feel free to use the button below whenever you are in a hurry and don't have time to learn everything!
In this section you can ask our expert robot anything related to the questions you encountered during the lessons! Feel free to use the button below whenever you need additional explanations!