Author : Noman Rauf
Page : 1
This tutorial will teach you about the four very important C/C++ string functions: strcmp(), strcpy(), strcat() and strlen().
All of these functions require the header <cstring> (<string.h> in older compilers which do not support Standard C++). Lets take a look at them turn by turn.
1. strcmp:
General form of calling strcmp() is:
strcmp(s1, s2);
strcmp() compares two strings and returns 0 (zero) if both are equal. If s1 is greater than s2 lexicographically (i.e. in dictionary order), it returns a positive value, if s1 is less than s2 it returns a negative value.
The following is a security program which asks the user to enter a password and then checks it against a password (which in this case is “password”).
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
char input[80];
cout << “Enter password: “;
gets(input);
if(!strcmp(input, “password”)) cout << “Password Accepted!\n”;
else cout << “Password Denied!\n”;
return 0;
}
Note that the return value of strcmp() is 0 (zero) which is false. So to check if two strings are equal or not we use the statement if(!strcmp(s1, s2)). To check for other return values use these statements:
1. Is s1 greater than s2 if(strcmp(s1, s2)>0);
2. Is s1 less than s2 if(strcmp(s1, s2)<0);
3. Is s1 equal to s2 if(!strcmp(s1, s2));
2. strcpy:
strcpy() copies the contents of one string to another destroying the contents of the string being copied to. General form of calling strcpy is:
strcpy(to, from);
As mentioned above, strcpy() copies the contents of one string to another. In this case, strcpy() copies the contents of the string from to to. Destroying anything already stored in the string to. It is to remember that the string to must be large enough to hold the string from or it will be overrun which may crash your program.
The following program demonstrates how to use strcpy():
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[50];
strcpy(str, “Hello”);
cout << str << “\n”;
strcpy(str, “ There”);
cout << str << “\n”;
return 0;}
The output of this program is:
Hello
There
Note that the previous contents of str were destroyed after copying “ There” to it.
3. strcat:
General form of calling strcat() is:
strcat(s1, s2);
The strcat() function is very simple. It just appends s2 to the end of s1. It could just be well said that strcat() concatenates s2 to the end of s1. The following program will demonstrate its use.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[50], s2[50];
strcpy(s1, “Hello”);
strcpy(s2, “ There”);
strcat(s1, s2);
cout << s1;
return 0;
}
This program produces the result:
Hello There
4. strlen:
To call strlen() use the form:
strlen(s);
It is the most simple function. It return the length of the string (s) in characters. The following program will show its use:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
cout << “Enter a string: “;
gets(str);
cout << “Length is: << “ strlen(str);
return 0;
}
To put all together, this program illustrates the use of all four string functions:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s1[80], s2[80];
cout << “Enter first string: “;
gets(s1);
cout << “Enter second string: “;
gets(s2);
cout << “Length of first string: “ << strlen(s1) << “\n”;
cout << “Length of second string: “ << strlen(s2) << “\n”;
if(!strcmp(s1, s2)) cout << “Strings are equal!\n”;
else cout << “Strings are not equal!\n”;
strcat(s1, s2);
cout << s1 << “\n”;
strcpy(s1, s2);
cout << s1 << “ and “ << s2 << “ are now the same!\n”;
return 0;
}
After running this program, if “hello” and “there” are entered, then the output will be:
Length of first string: 5
Length of second string: 5
Strings are not equal!
HelloThere
There and There are now the same!
Exercise:
Write a program that inputs a string and then encodes it by taking the characters from each end, starting with the left side and alternating, stopping when the middle of the string has been reached. For example, “Hi there” would be “Heir eth”.
Page : 1