example1_basic_operations.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cstring>
#include <iostream>

int main() {
    char str1[50] = "Hello";
    char str2[50] = "World";
    char result[100];

    // String length
    std::cout << "Length of str1: " << std::strlen(str1) << std::endl;

    // String copy
    std::strcpy(result, str1);
    std::cout << "Copied string: " << result << std::endl;

    // String concatenation
    std::strcat(result, " ");
    std::strcat(result, str2);
    std::cout << "Concatenated string: " << result << std::endl;

    // String comparison
    if (std::strcmp(str1, str2) == 0) {
        std::cout << "str1 and str2 are equal" << std::endl;
    } else {
        std::cout << "str1 and str2 are not equal" << std::endl;
    }

    return 0;
}
Back to cstring