string library

In this page, we will learn following,

 

string

The character of sequence or a sentence string is called like- Rahul sherma, good morning.

In C++ we use string classes to store a sequence of characters or a sentence. However, by using the character of array in C++ we can store a sentence, but by doing this we need an extra code of line which becomes slightly complicated and the program becomes a bit difficult to read. To overcome this difficulty, C++ has a pre-define function of string. To use them, we include their file string.h in the program.

 

How to read a string?

As we know, the cin statement is used to read user-input, but when we read a string here, we can not use the cin statement here.

To read the string, we use the get() and getline() function here, that is, when you use cin statement to store a string, cin space does not read the space. For example, if we want to store a name (Rahul sherma), then only Rahul will store in the cin statement whereas it will store the complete name.

The syntex of these statements is given below-

gets(variable);
cin.getline(variable, int);

Here is the Examples.

Both functions are defined within the header file stdio, so in a program we have to include their file (<stdio.h>).

 

use of string

  • No extra statement has to be written in the program. So that the readability of the program remains.
  • Because the extra code of line decreases, the size of the program also decreases.
  • In any program we use the inbuilt library function of the string, which saves time.

initialization of a string

char name[ ] = "Rahul";
char name[6] = {'R','a','h','u','l'}; 
char *name = "Rahul"; // using char pointer

a lot of library functions have been defined in the string class of C++. Some of these important function names and the tasks they perform are given in the table below.

 

some string library function

argu1 and argu2 are presenting string here.

Function name Used for
strcpy(argu1, argu2) copying one string to another variable
strcmp(argu1,argu2) comparing two string by each other
strlen(argu) for calculate string length, means how many character in it.
strupr(argu) convert string to uppercase
strlwr(argu) convert string to lowercase
strrev(argu) to reverse string
strstr(argu1,argu2) search a character in a string.
strcat(argu1,argu2) to add two string

Let’s try with an example

 

strcat(), strlen (), strupr(), strlwr(), strrev()

#include<iostream.h>
#include <string.h>
#include<conio.h>
#include<stdio.h>   // gets()

  void main()
   {
    clrscr();
    char name[10],last[10];
    char *comp;      // pointer variable declaration to hold string values

    cout<<"Enter first name: ";
    gets(name);
    cout<<"Enter last name : ";
    gets(last);

      comp = strcat(name,last);    //cout<< strcat(name,last);

      cout<<"\nstrcat : "<<comp;   // add two string

  int len = strlen(comp);

      cout<<"\nstrlen: "<<len;             // calculate string length

      cout<<"\nstrupr: "<<strupr(comp);    // convert string into uppercase

      cout<<"\nstrlwr: "<<strlwr(comp);    // convert string into lowercase

     cout<<"\nstrrev: "<<strrev(comp);    // reverse string
  
    getch();
 }
OUTPUT:-
Enter First name: rahul
Enter last name : sherma

strcat : rahul sherma
strlen : 12
strupr : RAHUL SHERMA
strlwr : rahul sherma
strrev : amrehs luhar

we can also manipulate string without string library function But for this, we should have a good skill in the control statement

 

copy two string using strcpy()

Two variables have been declared in the program, out of which the variable first takes input from the user while in variable second, we have copied the value of the variable first.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include <string.h>

void main()
{
  clrscr();
  char first[10],second[10];

  cout<<"Enter first name: ";
  gets(first);

  strcpy(second,first); //copying into last

  cout<<"value copied: "<<second;
  getch();
}
OUTPUT:-
Enter first name: rahul
value copied: rahul

 

compare two string using strcmp ()

This program is designed in a slightly different way. This program is a kind of log in wizard, in which user is asked for password. In this we have compared both passwords (enter password by user and true password) to each other from strcmp.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h> //getline)(
#include <string.h>

void main()
{
  start:     // goto statement used

  clrscr();
  char psd[10];
  char pwd[10]="password";

  cout<<"Enter Password: ";
  cin.getline(psd,10);

  if(strcmp(psd,pwd)==0)
  {
     cout<<"Matched..\n\n\t";
     system("pause");    // pause display screen with a message

  } else {

    cout<<"Try again..";
    goto start;     // transfer control if condition false
  }
}
OUTPUT:-
Enter Password: password
Matched..
       press any key to continue.

more about string

  • string with pointer

previous- Function and their type | Parameter type

next-Storage class and their types