C++ String Function

There re various string functions for that we need to include header file string.h

strcpy()


  • It stands for string copy.
  • This function is used to copy value of one string variable to another string variable.

Syntax


strcpy(strvar1,strvar2)

Program to show use strcpy() function.



#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    char name[25], sname[25];
    name="MorningStar";
    strcpy(sname,name);
    cout<<"\nName = "<<sname;
    getch();
}


Output


Name = MorningStar

strcat()


  • It stands for string concat.
  • This function is used to combine values of two string variable.

Syntax


strcat(strvar1,strvar2)

Program to show use strcat() function.



#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    char name[25], sname[25];
    name="Morning";
    sname="Star";
    strcpy(sname,name);
    cout<<"\nName = "<<sname;
    getch();
}


Output


Name = StarMorning


Note: As sname is at left and name is at right So, StarMoring

strlen()


  • It stands for string length.
  • This function is used to find number of characters in string variables.

Syntax


strlen(strvar)

Program to show use strlen() function.



#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    char name[25];
    int x;
    name="Morning";
    x=strlen(name);
    cout<<"\nNumber of characters = "<<x;
    getch();
}


Output


Number of characters = 7


strrev()


  • It stands for string reverse.
  • This function is used to reverse the order of characters stored in a string variable.

Syntax


strrev(strvar)

Program to show use strrev() function.



#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    char name[25];
    int x;
    name="Morning";
    strrev(name);
    cout<<"\nName = "<<name;
    getch();
}


Output


Name = gninrom


strcmp()


  • It stands for string compare.
  • This function is used to compare value of one string with another sting value.
  • If the value of both the strings are same, then it return 0
  • If the value of first string is larger than second string value, then it return negative value
  • If the value of first string is smaller than second string value, then it return positive value

Syntax


strcmp(strvar1,strvar2)

Program to show use strcmp() function.



#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    char name[25], sname[25];
    name="Morning";
    sname="Star";
    if(strcmp(sname,name)==0){
    cout<<"Same";
    }
    else{
     cout<<"Not Same";
    }
    getch();
}


Output


Not Same


strlwr()


  • It stands for string lower.
  • This function is used to convert the uppercase string to lower case

Syntax


strlwr(strvar1)

Program to show use strlwr() function.



#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    char name[25];
    name="MORNING";
    strlwr(name);
    cout<<"Name = "<<name;
    getch();
}


Output


Name  = morning


strupr()


  • It stands for string upper.
  • This function is used to convert the lowercase string to upper case

Syntax


strupr(strvar1)

Program to show use strupr() function.



#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    char name[25];
    name="morning";
    strupr(name);
    cout<<"Name = "<<name;
    getch();
}


Output


Name  = MORNING


Back To Top