C++ Strings

strcmp(): This function helps us to compare two strings. The comparision of string is performed character by character on the basis of ASCII values of the characters.

Syntax:
strcmp(s1, s2);

Returns
0 if s1 and s2 are the same
less than 0 if s1<s2
greater than 0 if s1>s2

#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;
	int a;
	a=strcmp(n1,n2);
	if(a==0)
	cout<<"Both the strings are same/equal";
	else
	  if(a>0)
	  cout<<"string is "<<n1<<" greater";
	  else
	  cout<<"string is "<<n2<<" greater";
	return(0);
}

strlwr() : This function helps us to convert the string to lower case. That is all upper case alphabets are converted to lower case , rest remain as it is.

Syntax:
strlwr(s1);

Returns the string s1 in lower case.

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	char n[20];
	cout<<"Enter any string ";
	cin>>n;
	//1
	strlwr(n);
	cout<<"string in lower case is "<<n<<endl;
	
	//2
	cout<<"string in lower case is "<<strlwr(n)<<endl;

	return(0);
}

strupr() : This function helps us to convert the string to upper case. That is all lower case alphabets are converted to upper case , rest remain as it is.

Syntax:
strupr(s1);

Returns the string s1 in upper case.

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	char n[20];
	cout<<"Enter any string ";
	cin>>n;
	//1
	strupr(n);
	cout<<"string in upper case is "<<n<<endl;
	
	//2
	cout<<"string in upper case is "<<strupr(n)<<endl;

	return(0);
}

strrev() : This function helps us reverse the contents of the string.

Syntax:
strrev(s1);

Returns the string s1 in reverse.

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	char n[20];
	cout<<"Enter any string ";
	cin>>n;
	//1
	strrev(n);
	cout<<"string in reverse is "<<n<<endl;
	
	//2
	cout<<"string in reverse is "<<strrev(n)<<endl;

	return(0);
}