//Sample Program to demonstrate the use of different string functions
#include < iostream.h >	//For. cout
#include < string.h >		//For many of the string functions
#include < conio.h >
void main()
{
char name[50];		//Declare variables
char lastname[50];
clrscr();
cout << "Please enter your name: " ;  //Tell the user what to do
cin.getline(name, 50);	 //User gets to input strings with spaces or just to get strings after the                                                              	//user presses enter
if(!strcmp(" Nhep Bora", name))    //The ! means not, strcmpi returns 0 for equal strings
{
cout << "That's my name too." << endl; //Tell the user if its my name
}
else
{
cout << " That's not my name.";
}
cout << "What is your name in uppercase. .." << endl;
strupr(name); 		//strupr converts the string to uppercase
cout << name << endl;
cout << "And, your name in lowercase... " << endl;
strlwr(name); //strlwr converts the string to lowercase
cout << name << endl;
cout << "Your name is " << strlen(name) << " letters long " << endl;
cout << "your last name:";
cin.getline(lastname, 50); //lastname is also a string
strcat(name, " ");	//We want to space the two names apart
strcat(name, lastname);	//Now we put them together, we a space in the middle
cout <<
" full name is " << name; 	//Outputting it all...
 getch();
 }
<< GO BACK

Our aim is to provide information to the knowledge seekers.


comments powered by Disqus


Footer1