C++ supports wide range of functions that manipulate null-terminated strings:
strlen() : This functions helps to find length of the string.
Syntax:
strlen(s1);
Returns the length of string s1.
#include<iostream> #include<string.h> using namespace std; int main() { char n[20]; cout<<"Enter any string "; cin>>n; int len=strlen(n); cout<<"String length = "<<len; return(0); }
strcpy() : This function helps us to copy contents one string onto another string.
Syntax:
strcpy(s1, s2);
Copies string s2 into string s1.
#include<iostream> #include<string.h> using namespace std; int main() { char n1[20],n2[20]; cout<<"Enter any string "; cin>>n1; strcpy(n2,n1) ; cout<<"source string "<<n1<<endl; cout<<"copied string "<<n1<<endl; return(0); }
strcat(): This function helps us to add contents of one string after the existing contents of another string.
strcat(s1, s2);
Concatenates string s2 onto the end of string s1
#include<iostream> #include<string.h> using namespace std; int main() { char n1[20],n2[20],n3[40]; cout<<"Enter 1st string "; cin>>n1; cout<<"Enter 2nd string "; cin>>n2; strcpy(n3,n1); //strcat(n3," ");//for space strcat(n3,n2); cout<<"1st string "<<n1<<endl; cout<<"2nd string "<<n1<<endl; cout<<"Concatenated string "<<n3<<endl; return(0); }