0% found this document useful (0 votes)
16 views

04_string_handling_tutorial

The document provides an overview of string handling in C++, detailing popular libraries such as <string> and <cstring>, along with essential functions like strlen(), strcpy(), and strcat(). It includes practical examples demonstrating how to manipulate strings, including splitting names and converting between C-style strings and std::string. Additionally, it offers notes on best practices for using these libraries and functions.

Uploaded by

phuctran180406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

04_string_handling_tutorial

The document provides an overview of string handling in C++, detailing popular libraries such as <string> and <cstring>, along with essential functions like strlen(), strcpy(), and strcat(). It includes practical examples demonstrating how to manipulate strings, including splitting names and converting between C-style strings and std::string. Additionally, it offers notes on best practices for using these libraries and functions.

Uploaded by

phuctran180406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CSC10012

STRING HANDLING IN C++


FIT-HCMUS

Contents
1 Popular libraries for string handling 2
1.1 <string> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 <cstring> (string.h) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Essential string handling functions in <cstring> 3


2.1 strlen() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 strcpy() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 strcat() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4 strcmp() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.5 strchr() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.6 strstr() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.7 strtok() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.8 Others: atoi() from <cstdlib> and to_string() from <string> . . . . . . . . . . . . . . . . . . . . . 5

3 Practical example 6

4 Some notes 7

1
Fundamentals of Programming | CSC10012 Department of Knowledge Engineering

1 Popular libraries for string handling


1.1 <string>
• The <string> library is specifically disigned to work with strings.

• The data type used for representing strings in this library is called std::string.

• This library is a feature of C++.

• Example usage:

#include <iostream>
#include <string> // length()

using namespace std;

int main () {
string s = "";

cout << "Enter a sentence: ";


getline(cin, s); // s is a std::string -> getline(cin, s)

cout << "The content of s is: " << s << "\n";


cout << "The size of s is " << s.length() << " bytes.\n";

return 0;
}

• Learn more at: std::string C++

1.2 <cstring> (string.h)


• The <cstring> library supports string handling using C-style string (char s[256]).

• This library is available in both C and C++.

• Example usage:

#include <iostream>
#include <cstring> // strlen()

#define MAXLEN 256

using namespace std;

int main() {
char s[MAXLEN];

cout << "Enter a sentence: ";

Page 2 / 7
Fundamentals of Programming | CSC10012 Department of Knowledge Engineering

cin.getline(s, MAXLEN); // s is a C-style string -> cin.getline(s, MAXLEN)

cout << "The content of sentence is: " << s << "\n";
cout << "The size of sentence is: " << strlen(s) << " bytes.\n";

return 0;
}

• Learn more at: <cstring> (<string.h>)

2 Essential string handling functions in <cstring>


2.1 strlen()
• Returns the length of a C-style string.

• Example usage:

char s[256] = "Sample string";

int len_s = strlen(s); // len_s = 13

2.2 strcpy()
• Copies a C-style string, including the terminating null character.

• Example usage:

char s_src[] = "Hello World"; // Source


char s_des[256]; // Destination

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:

char str[256] = "Hello ";

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:

0: the contents of both strings are equal.


< 0: the first character that does not match has a lower value in s1 than in s2.
> 0: the first character that does not match has a greater value in s1 than in s2.

• Example usage:

char s1[256] = "hello";


char s2[256] = "hello";
char s3[256] = "hi";

int s1_vs_s2 = strcmp(s1, s2);


// s1_vs_s2 = 0 as s1 and s2 are equal

int s1_vs_s3 = strcmp(s1, s3);


// s1_vs_s3 < 0
// Less than 0 as the first character
// that does not match ‘e’ in s1 has lower value than ‘i’ in s2

2.5 strchr()
• Locates the first occurrence of a character in a C-style string.

• Example usage:

char str[] = "Hello world";


char* pch;
char find_char = ’u’;

pch = strchr(str, find_char);

if (pch == NULL) // str does not contain find_char


cout << "Not found " << find_char << " in str!";
else // find_char was found in str
cout << "Found " << find_char << " at " << pch - str << "!";

// Note: The position of find_char in str is (pch - str)

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

char str[] = "Hello World";


char* pch;
char find_str[] = "llo";

pch = strstr(str, find_str);

if (pch == NULL) // str does not contain find_str


cout << "Not found " << find_str << " in str!\n";
else // find_str was found in str
cout << "Found " << find_str << " at " << pch - str << "!\n";

// Note: The position of find_str in str starts from (pch - str)

2.7 strtok()
• Splits a C-style string into tokens.

• Example usage:

// Splitting the string str into tokens by the following delimiters:


// space, comma, period, and hyphen

char str[] = "- This, a sample string.";


char* pch;

pch = strtok(str, " ,.-");


while (pch != NULL) {
std::cout << pch << "\n";
pch = strtok(NULL, " ,.-");
}

/*
The Console output is:
This
a
sample
string
*/

2.8 Others: atoi() from <cstdlib> and to_string() from <string>


• atoi() converts a C-style string to an integer.

• Example usage:

// Include <cstdlib>
char str_num[10] = "50";

Page 5 / 7
Fundamentals of Programming | CSC10012 Department of Knowledge Engineering

int num = atoi(str_num); // num = 50

• to_string() and c_str() (from the library <string>) convert an integer to a C-style string.

• Example usage:

int num = 50;


char str_num[10];
strcpy(str_num, to_string(num).c_str()); // Now str_num contains "50"

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:

• Input: "Nguyen Van Ty"

• Output: "Ty Nguyen"

Solution:

// Method 1:
#include <iostream>
#include <cstring>

#define MAXLEN 256

using namespace std;

int main () {
char name[MAXLEN];
char split_name[10][MAXLEN]; // Contains tokens split from name
int i = 0; // Index of split_name
char* pch;

cout << "Enter a name: ";


cin.getline(name, MAXLEN);

pch = strtok (name, " ");


strcpy(split_name[i++], pch);

while (pch != NULL) {


strcpy(split_name[i++], pch);
pch = strtok (NULL, " ");
}

cout << split_name[i-1] << " " << split_name[0];

Page 6 / 7
Fundamentals of Programming | CSC10012 Department of Knowledge Engineering

return 0;
}

// Method 2: (Advanced)
#include <iostream>
#include <cstring>

#define MAXLEN 256

using namespace std;

int main () {
char name[MAXLEN], last_name[MAXLEN], first_name[MAXLEN];
char* first_space;
char* last_space;

cout << "Enter a name: ";


cin.getline(name, MAXLEN);

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

cout << first_name << " " << last_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

You might also like