Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.
Fun, engaging games to boost memory, math fluency, typing speed, and English skillsβperfect for learners of all ages.
Enroll to start learning
Youβve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take mock test.
Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.
The section covers common string functions such as strlen, strcpy, strcat, and strcmp. It explains how each function operates on strings and provides examples to demonstrate their use.
This section introduces several vital string functions provided by the <cstring>
library in C++. These functions allow for fundamental operations on strings, facilitating their manipulation in programs. Understanding how to use these functions is crucial for effective string processing, which plays a significant role in data handling and text operations in programming.
Dive deep into the subject with an immersive audiobook experience.
Signup and Enroll to the course for listening the Audio Book
Function Description
strlen(str) Returns length of the string
strcpy(dest, src) Copies one string into another
strcat(str1, str2) Concatenates two strings
strcmp(str1, str2) Compares two strings lexicographically
In C++, the
1. strlen(str): This function returns the length of a string without counting the null character at the end of the string.
2. strcpy(dest, src): This function copies the contents of the source string (src) into the destination string (dest).
3. strcat(str1, str2): This function concatenates (appends) the second string (str2) to the end of the first string (str1).
4. strcmp(str1, str2): This function compares two strings lexicographically, meaning it checks their alphabetical order.
Think of the string functions as tools in a toolbox. Just as you might have one tool to measure a length (like a ruler), another tool to copy a design (like a photocopier), a mechanism to attach one object to another (like glue), and a scale to compare weights (like a weighing scale), these string functions help you manage and manipulate strings in programming effectively.
Signup and Enroll to the course for listening the Audio Book
Example
cpp
char str1[20] = "Hello";
char str2[20] = "World";
strcat(str1, str2); // str1 becomes "HelloWorld"
In this example, we declare two character arrays (strings):
- str1 is initialized with "Hello" and
- str2 is initialized with "World". By using the strcat()
function, we concatenate str2 to str1. After this operation, str1 will hold the value "HelloWorld". This demonstrates how string functions can modify existing strings by combining them.
Imagine you have a first name 'Hello' written on a piece of paper and a last name 'World' on another piece of paper. If you want to merge them to form a full name 'HelloWorld', you take the two pieces of paper and write them down on one sheet together. That's like what strcat()
does for strings in your code.