CodesJava

Easy learning with example program codes

CPP String Functions


String Functions are the built-in functions provided by CPP to operate and manipulate a string. CPP provides a number of string functions. Some of the MOST USED string functions are listed below:

 

FUNCTION SYNTAX USES
strcpy() strcpy(destination, source) Copies a string from source to destination.
strcat() strcat(string1, string2) Concatenates two strings.
strcmp() strcmp(string1, string2) Compares two strings, if same, it returns 0.
strlen() strlen (string name) Returns the length of a string.

 

Example:

#include <iostream.h>  
#include<cstring.h>
using namespace std;  
int main( )
{  
char chr1[] = "ROSE";  
char chr2[10];    
int l2 = strlen(chr1);
cout << "Hint: Number of words in my favourite flower name is: " << l2 <<endl;
cout << "Enter the name of my favourite flower: ";
cin >> chr2;
if (strcmp (chr1, chr2) == 0)
cout << "Right Answer!!";
return 0;
}

Output

Hint: Number of words in my favourite flower name is: 4     
Enter the name of my favourite flower: ROSE                 
Right Answer!!
Please follow and like us:
Posted in CPP   


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki