codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

80 / 87 topics
76<iostream> Reference77<fstream> Reference78<cmath> Reference79<string> Reference80<cstring> Reference81<ctime> Reference82<vector> Reference83<algorithm> Reference
Tutorials/C++ Programming/&lt;cstring&gt; Reference
⚡C++ Programming

&lt;cstring&gt; Reference

Updated 2026-05-12
30 min read

&lt;cstring&gt; Reference

The &lt;cstring&gt; library in C++ provides a set of functions for manipulating C-style strings. These functions are essential for tasks such as string length calculation, copying, concatenation, comparison, and tokenization. Understanding these functions is crucial for any C++ programmer dealing with text data.

Introduction

C-style strings are arrays of characters terminated by the null character ('\0'). The &lt;cstring&gt; library offers a variety of functions to perform operations on these strings efficiently. In this tutorial, we will explore some of the most commonly used functions: strlen, strcpy, strcat, strcmp, strncpy, memcpy, memset, and strtok.

Core Content

1. strlen

The strlen function calculates the length of a C-style string (excluding the null terminator).

Syntax

C++
1size_t strlen(const char *str);
  • Parameters: A pointer to the C-style string.
  • Return Value: The number of characters in the string.

Example

strlen_example.cpp
1#include <iostream>
2#include <cstring>
3
4int main() {
5 const char str[] = "Hello, World!";
6 std::cout << "Length of "" << str << "" is " << strlen(str) << std::endl;
7 return 0;
8}
Output

3. strcat

The strcat function appends a string to the end of another string.

Syntax

C++
1char *strcat(char *dest, const char *src);
  • Parameters:
    • dest: Pointer to the destination array where the content is to be appended.
    • src: C-style string to append.
  • Return Value: A pointer to the destination string.

Example

strcat_example.cpp
1#include <iostream>
2#include <cstring>
3
4int main() {
5 char dest[50] = "Hello, ";
6 const char src[] = "World!";
7 strcat(dest, src);
8 std::cout << "Concatenated string: " << dest << std::endl;
9 return 0;
10}
Output

5. strncpy

The strncpy function copies up to a specified number of characters from one string to another.

Syntax

C++
1char *strncpy(char *dest, const char *src, size_t n);
  • Parameters:
    • dest: Pointer to the destination array where the content is to be copied.
    • src: C-style string to copy.
    • n: Maximum number of characters to copy.
  • Return Value: A pointer to the destination string.

Example

strncpy_example.cpp
1#include <iostream>
2#include <cstring>
3
4int main() {
5 char dest[50];
6 const char src[] = "Hello, World!";
7 strncpy(dest, src, 5);
8 dest[5] = ''; // Null-terminate the string manually
9 std::cout << "Copied string: " << dest << std::endl;
10 return 0;
11}
Output

7. memset

The memset function sets a specified number of bytes to a given value.

Syntax

C++
1void *memset(void *str, int c, size_t n);
  • Parameters:
    • str: Pointer to the block of memory to fill.
    • c: Value to set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
    • n: Number of bytes to be set to the value.
  • Return Value: A pointer to the memory area str.

Example

memset_example.cpp
1#include <iostream>
2#include <cstring>
3
4int main() {
5 char str[50];
6 memset(str, 'A', 10); // Set first 10 bytes to 'A'
7 str[10] = ''; // Null-terminate the string manually
8 std::cout << "String after memset: " << str << std::endl;
9 return 0;
10}
Output

Practical Example

Let's create a simple program that reads a sentence from the user and counts the number of words using strtok.

word_count.cpp
1#include <iostream>
2#include <cstring>
3
4int main() {
5 const int max_length = 100;
6 char sentence[max_length];
7 std::cout << "Enter a sentence: ";
8 std::cin.getline(sentence, max_length);
9 const char *delim = " ";
10 char *token = strtok(sentence, delim);
11 int word_count = 0;
12 while (token != NULL) {
13 ++word_count;
14 token = strtok(NULL, delim);
15 }
16 std::cout << "Number of words: " << word_count << std::endl;
17 return 0;
18}

Summary

FunctionDescription
strlenCalculates the length of a C-style string.
strcpyCopies one string to another.
strcatAppends one string to another.
strcmpCompares two strings lexicographically.
strncpyCopies up to a specified number of characters from one string to another.
memcpyCopies a specified number of bytes from one memory location to another.
memsetSets a specified number of bytes to a given value.
strtokBreaks a string into tokens based on a specified delimiter.

What's Next?

Now that you have a solid understanding of the &lt;cstring&gt; library, it's time to explore date and time manipulation with the &lt;ctime&gt; library. This will allow you to work with dates, times, and perform various time-related operations in your C++ programs.


Previous<string> ReferenceNext <ctime> Reference

Recommended Gear

<string> Reference<ctime> Reference