C++ String Function


C String function supports all the string functions in C++.

 

There are commonly used string functions in C++. They are

  • strcat(): it concatenates str2 at the end of str1.
  • strcmp(): it returns 0 if str1 is same as str2.
  • strcpy(): it copies str2 into str1.
  • strlen(): it gives the length of str1.
  • strncpy(): it copies given the number of characters of one string to another.
  • strchr(): it returns the pointer to the first occurence of char in str1.
  • strstr(): it returns the pointer to the first occurence of str2 in str1.

Example:

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

int main () {
    char str1[30] = "Welcome to";
    char str2[30] = " Programtpoint";
    char str3[50];
    /* concatenates str1 and str2 */
    strcat( str1, str2);
	cout << "String concatenate str1 & str2: " << str1 << endl;
    /* copy str1 into str3 */
    strcpy( str3, str1);
    cout << "String copy from str1 to str3: " << str3 ;
    return 0;

}



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.