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 #include 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 #include #include 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<
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.
Now if we give the Name "Learning Hints" then its store whole String with spaces.#include #include #include 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<
#include #include #include 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<
To store more the one name we store the 2D array. For example
#include #include #include 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<
Declaring and Initializing Local Variable in C
Declaring and Initializing Global Variable in C Language
Printf Function In C Language
Sacnf Function In C Language
for Loop in C Language
While Loop in C Language
Do While Loop in C Language
Switch Case Statement IN C
IF -Else Statement IN C
Break Statement IN C /C++
User Define Function IN C /C++
Cout/Cin Function IN C /C++
Array IN C /C++
Two Dimensional Array IN C /C++
String In IN C /C++
Pointer In IN C /C++
Const and volatile keyword IN C /C++
Class and Object in C++
Function Over Loading in C++
Character Array In C++
Constructor in C++
Operator overloading in C++
Function Templates in C++
ATM Sample Project In Simple C Language
Find Area of Shape Using Function In C++