C/C++ String

How We Use String In IN C /C++

In C/C++ string is array of character. These character store consecutive memory location and the last character is an special character (Null character ?\0?) which indicates the end of the string.
For Example

char name [20] ={'L','E','A','R','N','I','N','G',?\0?};
or
char name [20] ="LEARNING";
or
char name [ ] ="LEARNING";
For example: (Storing The single Name using character array)



#include<stdio.h>
#include<conio.h>
void main(void)
 {
char name[8];  // we can store max 8 character
int i;//Declaration of Other variable which use in two loop .
for(i=0  ;i<=7  ;  i++)
scanf("%c ",&name[i];// this statement will get the name[0],name[1]
             //and up to so on . we will get 8 character including space
for(i=0  ;i<=7  ;  i++)
printf("%c",name[i]) ; // this statement will display the
          // name[0], name[1] and up to so on display 8 character .
getch( ) ;
}

The Drawback in above example we must be enter the 8 character Name because loop must be run 8 time.
Now we take an other example to handle this drawback.


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main(void)
 {
char name[20];  // we can store max 20 character
cout<<"enter the name= ";   // Display the simple message
cin>>name;// Get the Name as string when we press space
         //  or press Enter key  our input is complete. 
		 //  its means it did not get space.
cout<<endl<<"You Enter The Name"<<name;
getch( ) ;
}

Now its can give max 20 character but a Drawback in above example is that its not take a space between Name.
For example if we input "Learning Hints" then its only store the "Learning"
Now we take an other example to handle this drawback.


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main(void)
 {
char name[20];  // we can store max 20 character

cout<<"enter the name= ";   // Display the simple message

gets(name);// Gets is an string input statement 
            //which get the string including space 
			//when we press enter key our input is complete
			
cout<<endl<<"You Enter The Name"<<name;

getch( ) ;
}

Now if we give the Name "Learning Hints" then its store whole String with spaces.

To read the text containing blank space, cin.get function can be used. This function takes two arguments. First argument is the name of the string (address of first element of string) and second argument is the maximum size of the array.
For Example


#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main(void)

 {

char name[20];  // we can store max 20 character

cout<<"enter the name= ";   // Display the simple message

cin.get(name ,100);// Gets is an string input statement
                   // which get the string including space when we press //enter key our input is complete
				   
cout<<endl<<"You Enter The Name"<<name;

getch( ) ;
}

To store more the one name we store the 2D array. For example



#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main(void)
 {
char name[4][20];  // we can store 4 name with maximum  20 character
cout<<"enter the name= ";   // Display the simple message

for(i=0;i<=3;i++)// Set the loop for 4 time

gets(name[i]);// Gets is an string and store in name[0], name[1],name[2] and name[3]

cout<<endl<<"You Enter The Name are:"<<endl;// Display simple message

for(i=0;i<=3;i++)// Set the loop for 4 time

cout<<name[i]<<endl;// Display the string value of name[i] , where 1 is 0 to 3

getch( ) ;

}