String handling functions in C language

C++ provides a number of built-in functions for string handling. Some commonly used functions are:

Function
Description
strlen()
Returns the length of a string
strcpy()
Copies one string to another
strcat()
Concatenates (joins) two strings
strncat()
Concatenates a specified number of characters from one string to another
strcmp()
Compares two strings
strncmp()
Compares a specified number of characters from two strings
strchr()
Searches for the first occurrence of a character in a string
strstr()
Searches for the first occurrence of a substring in a string
atoi()
Converts a string to an integer

strlen(): Returns the length of a string.

Example:

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char str[] = "Hello, world!";
    int len = strlen(str);
    cout << "The length of the string is " << len << endl;
    return 0;
}

Output:

The length of the string is 13

strcpy(): Copies one string to another.

Example:

#include <iostream>
#include <cstring>
using namespace std;

int main() {
char str1[] = "Hello, world!";
char str2[50];
strcpy(str2, str1);
cout << "Copied string is: " << str2 << endl;
return 0;
}

Output:

Copied string is: Hello, world!

strncpy(): Copies a specified number of characters from one string to another.

Example:

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    char str1[] = "Hello, world!";
    char str2[50];
    strncpy(str2, str1, 5);
    str2[5] = '\0'; // add null character at the end
    cout << "Copied string is: " << str2 << endl;
    return 0;
}

Output:

Copied string is: Hello

strcat(): Concatenates (joins) two strings.

Example:

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char str1[] = "Hello";
    char str2[] = "world!";
    strcat(str1, str2);
    cout << "Concatenated string is: " << str1 << endl;
    return 0;
}

Output:

Concatenated string is: Helloworld!

strncat(): Concatenates a specified number of characters from one string to another.

Example:

#include <iostream>
#include <cstring>

int main() {
    char str1[] = "Hello";
    char str2[] = "world!";
    strncat(str1, str2, 3);
    std::cout << "Concatenated string is: " << str1 << std::endl;
    return 0;
}

Output:

Concatenated string is: Helloworld

strcmp(): Compares two strings.

Example:

#include <iostream>
#include <cstring>
using namespace std;

int main() {
   char str1[] = "apple";
   char str2[] = "banana";

   int result = strcmp(str1, str2);

   if (result < 0) {
      cout << str1 << " comes before " << str2 << " in dictionary" << endl;
   } else if (result > 0) {
      cout << str1 << " comes after " << str2 << " in dictionary" << endl;
   } else {
      cout << "Both strings are equal" << endl;
   }

   return 0;
}

Output:

apple comes before banana in dictionary

strncmp(): Compares a specified number of characters from two strings

#include <iostream>
#include <cstring>

using namespace std;

int main() {
   char str1[] = "apple pie";
   char str2[] = "apple juice";

   int result = strncmp(str1, str2, 5);

   if (result < 0) {
      cout << str1 << " comes before " << str2 << " in dictionary" << endl;
   } else if (result > 0) {
      cout << str1 << " comes after " << str2 << " in dictionary" << endl;
   } else {
      cout << "Both strings are equal up to 5 characters" << endl;
   }

   return 0;
}

Output:

Both strings are equal up to 5 characters

strchr(): Searches for the first occurrence of a character in a string

#include <iostream>
#include <cstring>
using namespace std;

int main() {
   char str[] = "hello world";
   char *result = strchr(str, 'w');

   if (result != NULL) {
      cout << "The character 'w' was found at position " << result - str + 1 << endl;
   } else {
      cout << "The character 'w' was not found" << endl;
   }

   return 0;
}

Output:

The character 'w' was found at position 7

strstr(): Searches for the first occurrence of a substring in a string

#include <iostream>
#include <cstring>

int main() {
    char str1[] = "The quick brown fox jumps over the lazy dog";
    char str2[] = "brown";

    char *result = strstr(str1, str2);

    if (result != nullptr) {
        std::cout << "The substring '" << str2 << "' was found at position " 
                  << result - str1 + 1 << std::endl;
    } else {
        std::cout << "The substring '" << str2 << "' was not found" << std::endl;
    }

    return 0;
}

Output:

The substring 'brown' was found at position 11

atoi(): Converts a string to an integer

#include <iostream>
#include <cstdlib>

int main() {
    char str[] = "123";
    int num = atoi(str);
    std::cout << "The integer is: " << num << std::endl;
    return 0;
}

Output:

The integer is: 123