String functions (strcat, strchr, strstr, strcmp,strlen,strupr,strlwr)

 Stringscan be manipulated in different ways. there is a set of predefined functions in C++, which are used to manipulated strings in different manners. the predefined functions which are declared in the string.h( or cstring header file in new C++ compilers) header file.

Strings predefined functions are:

strcat()

strcat() function is used to concatenate two strings with each other. it appends the second string to the end of the other string.
sytax:
char *strcat(char *Destination const char *source);
Program to use strcat() funstion:

#include<iostream>
using namespace std;
#include<cstring>
int main()
{
char str1[10]="HELLO";
char str2[10]="world";
char str3[20];
strcat(str1,str2);
cout<<"the string after concatenation is : "<<str1;
}

output:
the string after concatenation is : HELLOworld

strchr()

strchr() function is a function used to scan a string in forward direction and looking for the first occurrence of a specific character given in the search criteria.
Program for strchr():

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char pn[20]="HELLO";
char tosearch;
cout<<"enter charater to search in given string : ";
cin>>tosearch;
char *p;
p=strchr(pn,tosearch);
if(p==NULL)
{
cout<<"character "<<tosearch<<" not found in "<<pn<<endl;
}
else
{
cout<<"character"<<tosearch<<"found in "<<pn<<endl;
}
return 0;
}  

output(not found):
enter capital character to search in given string : W
character W not found in HELLO

output(found):
enter capital charater to search in given string : E
character E found in HELLO

strcmp()

strcmp() function is used to compare to strings with each other either they are equal or not. This function is a case sensitive function and it only compares the same case strings. If both the strings are equal then this function return 0 otherwise , it returns 1 or -1.
Program for strcmp(): 
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char s[10]="Hello";
char s1[10]="hello";
int t;
t=strcmp(s,s1);
if(t==0)
{
cout<<"both strings are equal :";
}
else
{
cout<<"both strings are different";
}
}

output:
both strings are different

strcmpi()

strcmpi() function is used to compare to strings with each other either they are equal or not. This function is similar to strcmp() function but this function is not case sensitive function. It compares two functions without case sensitivity.
Program for strcmpi():

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char s[10]="Hello";
char s1[10]="hello";
int t;
t=strcmpi(s,s1);
if(t==0)
{
cout<<"both strings are equal :";
}
else
{
cout<<"both strings are different";
}
}

output:
both strings are equal

strcpy()

strcpy() function is used to copy a portion of source string to a destination.
Program for strcpy():

#include<iostream>
using namespace std;
#include<cstring>
int main()
{
char str1[10]="HELLO";
char str2[10]="world";
char str3[20];
strcpy(str3,str1);
cout<<" the result of strcpy(str3,str1) is : "<<strcpy(str3,str1);
return 0;
}

output:
the result of strcpy(str3,str1) is : HELLO

strrev()

strrev() function is used to reverse a given string. The strrev() function takes the string as argument and return its argument.
program for strrev() function:

#include<iostream>
using namespace std;
#include<cstring>
int main()
{
char str1[10]="HELLO";
strrev(str1);
cout<<" the result of strrev(str1) is : "<<str1;
return 0;
}
 
output:
 the result of strrev(str1) is : OLLEH

strlwr()

strlwr() function is used to convert a string from upper case characters  to a lower case character. It takes a string as an argument and return its lowercase.
Program for strlwr() function:
#include<iostream>
using namespace std;
#include<cstring>
int main()
{
char str1[10]="HELLO";
strlwr(str1);
cout<<" the result of strlwr(str1) is : "<<str1;
return 0;
}

output:
the result of strlwr(str1) is : hello

strupr()

strupr() function is used to convert a string form upper to its lowercase characters.
Program for strrev() function:

#include<iostream>
using namespace std;
#include<cstring>
int main()
{
char str1[10]="hello";
strupr(str1);
cout<<" the result of strupr(str1) is : "<<str1;
return 0;
}

Output:
the result of strupr(str1) is : HELLO

strlen()

strlen() function is used to find the length of a given string. The strlen() function takes a string as an argument and returns its length as an integer.
Program for strlen();

#include<iostream>
using namespace std;
#include<cstring>
int main()
{
char str1[10]="hello";
int i;
i=strlen(str1);
cout<<" the result of strlen(str1) is : "<<i;
return 0;
}

Output:
 the result of strlen(str1) is : 5

Comments