Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Master C++ String Functions: strcpy, strcat, strlen, strcmp – Practical Examples

What Is a String?

A string is simply a sequence of characters. In modern C++ we usually work with objects of the std::string class, which handles memory allocation, resizing, and a host of useful member functions automatically. This eliminates the need for manual buffer management and reduces the risk of buffer overflows.

For educational purposes, C++ still supports the traditional C‑style null‑terminated character arrays. Both approaches have their place: use std::string for everyday programming, and reserve C‑style strings for legacy APIs or performance‑critical low‑level code.

Declaring Strings

C‑Style Character String

Example:

char name[5] = {'J', 'o', 'h', 'n', '\0'};

or more concisely:

char name[] = "John"; // compiler inserts the terminating '\0'

std::string

To use std::string, include the header and declare a variable:

#include <string>

std::string name = "John";

Accessing String Values

Below is a simple console program that prints a C‑style string and an std::string value.

#include <iostream>
#include <string>

using namespace std;

int main() {
    char cName[5] = "John";          // C‑style
    string cppName = "Guru99";       // std::string

    cout << "C‑style string: " << cName << endl;
    cout << "std::string: " << cppName << endl;

    return 0;
}

Output:

Master C++ String Functions: strcpy, strcat, strlen, strcmp – Practical Examples

Key C String Functions

These functions are defined in the cstring header. They operate on C‑style strings (null‑terminated arrays of char).

strcpy()

Copies the source string into the destination buffer. The destination must be large enough to hold the source.

strcpy(dest, src); // dest must have enough space

strcat()

Appends the source string to the end of the destination string. Again, the destination buffer must be large enough to accommodate the result.

strcat(dest, src); // dest must have enough space

strlen()

Returns the number of characters in the string, excluding the terminating null byte.

size_t len = strlen(str);

strcmp()

Compares two strings lexicographically. Returns 0 if they are equal, a negative value if the first string is less, and a positive value if the first string is greater.

int cmp = strcmp(str1, str2);

Practical Example

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    char name1[10] = "Guru99";
    char name2[10] = "John";
    char name3[20]; // Ensure it can hold the concatenated result

    // Copy
    strcpy(name3, name1);
    cout << "strcpy(name3, name1): " << name3 << endl;

    // Concatenate
    strcat(name1, name2);
    cout << "strcat(name1, name2): " << name1 << endl;

    // Length
    size_t len = strlen(name1);
    cout << "strlen(name1): " << len << endl;

    // Comparison
    int cmp = strcmp(name1, name3);
    cout << "strcmp(name1, name3): " << cmp << endl;

    return 0;
}

Output:

Master C++ String Functions: strcpy, strcat, strlen, strcmp – Practical Examples

Takeaway

C Language

  1. C Programming Strings: Mastering Declaration, Initialization, and I/O
  2. Master Python Strings: Creation, Formatting, and Manipulation
  3. Mastering Java Strings: Creation, Methods, and Best Practices
  4. C++ do‑while Loop: Syntax, Practical Examples, and Nested Loops
  5. Mastering C++ Pointers: Concepts, Examples & Practical Applications
  6. C++ Operator Overloading – A Practical Guide with Code Examples
  7. C Strings: How Null-Terminated Character Arrays Work
  8. Mastering C++ Strings: C-Style vs std::string
  9. Mastering Strings in C#: Best Practices & Tips
  10. Mastering Python Strings: Creation, Access, and Manipulation