country the name: INDIA The country name is INDIA //Program to demonstrate a character array. Initialization The character array can be initialized at the time of its declaration. The syntax is shown below: char [size]={ list of characters separated by comma or a string } ; For example, char country[ ]=“INDIA”; In the above example, the text “INDIA” has letters which is assigned as initial value to array country. The text is enclosed within double quotes.
The memory representation is shown in Figure . Chapter Page - - I Country[ ] Country[ ] Country[ ] Country[ ] Country[ ] Country[ ] N D I A '\ ' Figure . In the above memory representation, each character occupies one byte in memory. At the end of the string, a null character is automatically added by the compiler.
C++ also provides other ways of initializing the character array: char country[ ]={‘I’, ‘N’, ‘D’, ‘I’, ‘A’, ‘\ ’}; char country[]= “ INDIA”; char country[]={‘I’, ‘N’, ‘D’, ‘I’, ‘A’, ‘\ ’}; If the size of the array is not explicitly mentioned, the compiler automatically calculate the size of the array based on the number of elements in the list and allocates space accordingly. In the initialization of the string, if all the characters are not initialized, then the rest of the characters will be filled with NULL. Example: char str[ ]={' ','+','A'}; str[ ]; ---> str[ ]; ---> + str[ ]; ---> A str[ ]; ---> NULL str[ ]; ---> NULL Note During initialization, the array of elements cannot be initialized more than its size. For example char str[ ]={' ','+','A','B'}; // Invalid In the above example, the compiler displays “initialize-string for array of chars is too long” error message.
#include<iostream> using namespace std; int main( ) int i, j, len, flag = ; char a [ ]; cout<<"Enter a string:"; cin>>a; for(len= ;a[len]!='\ ';++len) for(!= ,j=len- ;i<len/ ;++i,--j) if(a[j]!=a[i]) flag= ; Write a Program to check palindrome or not Chapter Page - - if(flag== ) cout<<"\n The String is palindrome"; else cout<<"\n The String is not palindrome"; return ; Output: Enter a string : madam The String is palindrome . Two-dimensional array Two-dimensional (2D) arrays are