11th Computer Science 12th Computer Science BCA CBSE JAC JAC

String Functions in C++ with Example

String in C++
Written by AIPS

There are various string handling functions in C++ Language which provides facility to handle string or string manipulation in different ways.

All functions of String handling in C++ have defined in header file #include<string.h>

example

strlen(), strcat(), strcpy(), strlwr(), strupr(), etc..

characters Represented in double quote or inverted comma known as string.

Example

“Ram” “225” “A” “5” etc..

String Functions

  • strlen():- It is used to find length of the string.
    • syntax strlen(Str);
  • strcat():- This function is used for concatenating two strings.
    • syntax strcat(Str1, Str2);
  • strcpy():- This function is used for copying of one string to another string.
    • syntax strcpy(Str1,Str2);
  • strupr():- This function converts into uppercase.
    • syntax strupr(Str);
  • strlwr():- This functions converts into lowercase.
    • syntax strlwr(Str);
  • strrev():- This function is used to reverse of the string.
    • syntax strrev(Str);
  • strcmp():- This function is used for comparison of two strings.It is case sensitive.
    • syntax strcmp(Str1,Str2);
  • stricmp():- This compares two strings by ignoring cases. According to this method “RAM” and “Ram” are same.
    • syntax stricmp(Str1,Str2);

Note:

During process of comparison following cases may occur.

case 1: if str1 and str2 are same then output becomes zero.

case 2:- if str1 is greater then output is greater than zero (positive value).

case 3:- if str2 is greater then output is less than zero(Negative value)

Q. Write a program to enter String (name) and print them.

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();
char str[20]; 
cout<<”Enter any string=”;
cin>>str;
cout<<”String =”<<str;
getch();
}

Output
Enter any string=Ram kumar
String =Ram
Note :- cin>> does not allow to enter full string (more than one word)
Note :- we use gets () function of #include for full string(more than word)

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()  
{
clrscr();
char str[20]; 
cout<<”Enter any string=”;
gets(str);
cout<<”String =”<<str;
getch();
}

Output
Enter any string= Ram kumar
String = Ram kumar

Q.Write a program to find length of the string . or Write a program to represent strlen( ) function.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str[20]; 
cout<<”Enter any string=”;
gets(str);
cout<<”length of string =”<<strlen(str);
getch();
}

OR

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str[20]; 
int l;
cout<<”Enter any string=”;
gets(str);
l=strlen(str);
cout<<”length of string =”<<l;
getch();
}

Output
Enter any String= Ram kumar
length of string =9
Note: Ram (3 letter) Kumar(5 letter) and between space 1 letter

Q.Write a program to convert lowercase string into uppercase string.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str[20]; 
cout<<”Enter any string in lowercase=”;
gets(str);
cout<<”Uppercase string =”<<strupr(str);
getch();
}

Output
Enter any string in lowercase= aips
Uppercase string =AIPS

Q.Write a program to convert uppercase string into lowercase string.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str[20]; 
cout<<”Enter any string in uppercase=”;
gets(str);
cout<<”lowercase string =”<<strlwr(str);
getch();
}

Output
Enter any string in uppercase=AIPS
lowercase string=aips

Q.Write a program to Reverse of input string.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str[20]; 
cout<<”Enter any string =”;
gets(str);
cout<<”reverse string =”<<strrev(str);
getch();
}

Output
Enter any string=AIPS
reverse=SPIA

Q.Write a program represent strcat() function

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str1[20], str2[20]; 
cout<<”Enter first string =”;
gets(str1);
cout<<”Enter second string =”;
gets(str2);

cout<<strcat(str1,str2);
getch();
}

Output
Enter first string=ram
Enter second string=sita
ramsita
Note:- After concatenation (adding) stores in str1.

OR

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str1[20], str2[20]; 
cout<<”Enter first string =”;
gets(str1);
cout<<”Enter second string =”;
gets(str2);
strcat(str1,str2);
cout<<str1;
getch();
}

Output
Enter first string=ram
Enter second string=sita
ramsita

Q.Write a program to represent strcmp( ) function.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str1[20], str2[20]; 
cout<<”Enter first string =”;
gets(str1);
cout<<”Enter second string =”;
gets(str2);

cout<<strcmp(str1,str2);
getch();
}

Output
Enter first string=ram
Enter second string=ram
0
Note:- Returns zero(0) if both the strings are same.

OR


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str1[20], str2[20]; 
cout<<”Enter first string =”;
gets(str1);
cout<<”Enter second string =”;
gets(str2);

cout<<strcmp(str1,str2);
getch();
}

Output
Enter first string=ram
Enter second string=Ram
32
Note:- Returns positive value if str1 is greater. (ASCII r- ASCII R)

OR

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str1[20], str2[20]; 
cout<<”Enter first string =”;
gets(str1);
cout<<”Enter second string =”;
gets(str2);

cout<<strcmp(str1,str2);
getch();
}

Output
Enter first string=Ram
Enter second string=ram
-32
Note:- Returns Negative value if str2 is greater. (ASCII R- ASCII r)

Q.Write a program to represent stricmp( ) function.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str1[20], str2[20]; 
cout<<”Enter first string =”;
gets(str1);
cout<<”Enter second string =”;
gets(str2);

cout<<stricmp(str1,str2);
getch();
}

Output
Enter first string=ram
Enter second string=Ram
0
Note:- compares after ignoring case. according to this function r=R

Q.Write a program to represent strcpy( ) function.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()  
{
clrscr();
char str1[20], str2[20]; 
cout<<”Enter first string =”;
gets(str1);

strcpy(str2,str1);
cout<<str2;
getch();
}

output
Enter first string=ram
ram
Note:- copies Right value to left.

12th Computer Science Important Program

C++ Programming

C++ Programming Language Tutorial

Translators in Programming languages

Tokens in C++ Programming Language

Operators in C++ Programming Language

Conditional statements in C++ Language

Switch Statement in C++ Language

Loops / Iteration statement in C++ Language

Jump statements in C++ with best example

Arrays in C++ Programming Language

Functions in C++ Programming Language

Passing Arguments in Functions

Method / Function overloading in C++

String Functions in C++ with Example

Boolean Algebra

Introduction to Boolean Algebra- Best concepts

Important Law of Boolean algebra with Best concepts

Logic Gates and their types -Best Concept

De Morgan’s Law and their Applications

Applications of Boolean Algebra in Computer Science

DBMS

Introduction to DBMS | Database Management System

Communication and Network Concept

Introduction to Communication and Networks

11th Computer Science

C++ Basic Program for 11th Computer Science

To be continued…. for more……check daily…………

About the author

AIPS

Leave a Comment