C++ String

String


  • String is a sequence of characters enclosed in double quotes.
  • String includes alphabets, digits, special characters and blank space.
  • Sting is always terminated with special character called null character ( ' \0 ' )
  • E.g. "Rahul" , "2024" , "$3%2" , "abc@10xmoningstar.com" , etc.

String Variable


A string is defined as group of characters in array.
e.g char name[25];

Syntax


char Str_var[Size]="Value";


Here, char is data type.
Str_var refers to String variable
Size refers to maximum size of string value
Value refers to value of string.

Program to initialize and display String



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


Output


Name  = morningstar

Program to read and display string



#include<iotream.h>
#include<conio.h>
void main()
{
     clrscr();
     char name[25];
     cout<<"Enter Name = ";
    cin>>name;
    cout<<"\n Name = "<<name;
     getch();
}


Output

Enter Name = Rahul
Name = Rahul

Back To Top