0% found this document useful (0 votes)
29 views

String Functions

This document contains examples of using string functions in C++ programs. It demonstrates copying strings with strcpy(), concatenating strings with strcat(), searching for characters within strings using strchr(), comparing strings with strcmp(), getting the length of a string with strlen(), and reversing a string with strrev(). The examples cover basic string manipulation tasks like inputting names, searching strings, guessing passwords, and displaying string lengths.

Uploaded by

Tribe Fighters
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

String Functions

This document contains examples of using string functions in C++ programs. It demonstrates copying strings with strcpy(), concatenating strings with strcat(), searching for characters within strings using strchr(), comparing strings with strcmp(), getting the length of a string with strlen(), and reversing a string with strrev(). The examples cover basic string manipulation tasks like inputting names, searching strings, guessing passwords, and displaying string lengths.

Uploaded by

Tribe Fighters
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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);

cout<<”copy of inputted string-🡪 “<<str_2;

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);

cout<<str_1<<” has “<<nol<<” letters.”;

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;
}

You might also like