String Functions
String Functions
Example:
#include <iostream.h>
#include <string.h>
#include <conio.h>
int main()
{
char str_1[20], str_2[20];
clrscr();
cout<<”enter a string (word): “;
cin>>str_1;
strcpy(str_2,str_1);
getch();
return 0;
}
Example:
#include <iostream.h>
#include <string.h>
#include <conio.h>
int main()
{
char full_name[30];
char gn[10], mn[10], fn[10];
clrscr();
cout<<”enter the following…”<<endl
<<”given name: “;
cin>>gn;
cout<<”middle name: “;
cin>>mn;
cout<<”family name: “;
cin>>fn;
strcpy(full_name,fn);
strcat(full_name,”, “);
strcat(full_name,gn);
strcat(full_name,” “);
strcat(full_name,mn);
cout<<endl<<full_name ;
getch();
return 0;
}
Example:
#include <iostream.h>
#include <string.h>
#include <conio.h>
int main()
{
char str_1[20], let, result;
clrscr();
cout<<”enter a string (word): “;
cin>>str_1;
cout<<”enter character to be searched in word: “;
cin>>let;
if (strchr(str_1,let))
{
cout<<let<<” is used in “<<str_1;
}
else
{
cout<<let<<” not found in “<<str_1;
}
getch();
return 0;
}
Example:
#include <iostream.h>
#include <string.h>
#include <conio.h>
int main()
{
char password[20], guess[20];
int count=3;
clrscr();
cout<<”set the password… “
<<”\nenter password to be guessed: “;
cin>>password;
clrscr();
cout<<”guess the password. You have 3 attemps…\n\n”;
while (count>0)
{
cout<<”enter your guess: “;
cin>>guess;
if (strcmp(password,guess)==0)
{
cout<<”you got it right.”;
break;
}
else
{
count--;
cout<<” you have “<<count<<”remaining attempt(s).”
}
}
if (count==0)
{
cout<<”better try again.”;
}
getch();
return 0;
}
Example:
#include <iostream.h>
#include <string.h>
#include <conio.h>
int main()
{
char str_1[20];
int nol;
clrscr();
cout<<”enter a string (word): “;
cin>>str_1;
nol=strlen(str_1);
getch();
return 0;
}
Example:
#include <iostream.h>
#include <string.h>
#include <conio.h>
int main()
{
char password[20], guess[20];
int count=3;
clrscr();
cout<<”set the password… “
<<”\nenter password to be guessed: “;
cin>>password;
strcpy(password,strrev(password));
clrscr();
cout<<”guess the password. You have 3 attemps…\n\n”;
while (count>0)
{
cout<<”enter your guess: “;
cin>>guess;
if (strcmp(password,guess)==0)
{
cout<<”you got it right.”;
break;
}
else
{
count--;
cout<<” you have “<<count<<”remaining attempt(s).”
}
}
if (count==0)
{
cout<<”better try again.”;
}
getch();
return 0;
}