04_string_handling_tutorial
04_string_handling_tutorial
Contents
1 Popular libraries for string handling 2
1.1 <string> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 <cstring> (string.h) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 Practical example 6
4 Some notes 7
1
Fundamentals of Programming | CSC10012 Department of Knowledge Engineering
• The data type used for representing strings in this library is called std::string.
• Example usage:
#include <iostream>
#include <string> // length()
int main () {
string s = "";
return 0;
}
• Example usage:
#include <iostream>
#include <cstring> // strlen()
int main() {
char s[MAXLEN];
Page 2 / 7
Fundamentals of Programming | CSC10012 Department of Knowledge Engineering
cout << "The content of sentence is: " << s << "\n";
cout << "The size of sentence is: " << strlen(s) << " bytes.\n";
return 0;
}
• Example usage:
2.2 strcpy()
• Copies a C-style string, including the terminating null character.
• Example usage:
strcpy(s_des, s_src);
// Now s_des contains "Hello World"
2.3 strcat()
• Appends a copy of the source string to the destination string.
• Example usage:
strcat(str, "World");
// Now str contains "Hello World"
Page 3 / 7
Fundamentals of Programming | CSC10012 Department of Knowledge Engineering
2.4 strcmp()
• Compares the C-style string s1 to the C-style string s2.
• Returns:
• Example usage:
2.5 strchr()
• Locates the first occurrence of a character in a C-style string.
• Example usage:
2.6 strstr()
• Locates the first occurrence of a C-style string s2 in C-style string s1.
• Example usage:
Page 4 / 7
Fundamentals of Programming | CSC10012 Department of Knowledge Engineering
2.7 strtok()
• Splits a C-style string into tokens.
• Example usage:
/*
The Console output is:
This
a
sample
string
*/
• Example usage:
// Include <cstdlib>
char str_num[10] = "50";
Page 5 / 7
Fundamentals of Programming | CSC10012 Department of Knowledge Engineering
• to_string() and c_str() (from the library <string>) convert an integer to a C-style string.
• Example usage:
3 Practical example
Problem: Input a string containing the full name of a person. Print the person’s name in the format: FirstName
LastName.
Example:
Solution:
// Method 1:
#include <iostream>
#include <cstring>
int main () {
char name[MAXLEN];
char split_name[10][MAXLEN]; // Contains tokens split from name
int i = 0; // Index of split_name
char* pch;
Page 6 / 7
Fundamentals of Programming | CSC10012 Department of Knowledge Engineering
return 0;
}
// Method 2: (Advanced)
#include <iostream>
#include <cstring>
int main () {
char name[MAXLEN], last_name[MAXLEN], first_name[MAXLEN];
char* first_space;
char* last_space;
first_space = strchr(name, ’ ’); // Find the first space to get last name
int len_last_name = first_space - name;
strncpy(last_name, name, len_last_name); // Copy last name from name to last_name
last_name[len_last_name] = ’\0’; // Assign the terminating null character
last_space = strrchr(name, ’ ’); // Find the last space to get first name
strcpy(first_name, last_space + 1); // Copy first name from name to first_name
return 0;
}
4 Some notes
1. When working with a std::string object named s → Use the <string> library (#include <string>), and
the input statement: getline(cin, s).
2. When working with a C-style string named s (char s[256]) → Use the <cstring> library (#include <cstring>),
and the input statement: cin.getline(str, MAXLEN).
3. Ensure there is a terminating null character (’\0’) at the end of C-style string.
4. When working with a std::string object, you can convert it to a C-style string using c_str() function.
This allows you to use functions from the <cstring> library. For more information, refer to: c_str C++.
Page 7 / 7