character array in C++

The sequence of characters is called string and character array in C++ is a collection of character or sequence of character, here in this page we will understand character array with the following Example,

The character array is used for the C string related task. The character array is declared just like int and float data type declared in the program.

syntax:

storage-class char array-name[size];

The character array and the integer array both are different, The character array always terminates with the NULL character while in the array is not such a condition.

Example

char name[6];

where name is an identifier.

here is the difference between character array and string.

Initialization of a character array

syntax

char array-name[size]= {list}; 
char name[] = "Rahul";

or

char array-name[size]= {list, '\0'};

char name[6]= {'R', 'a', 'h', 'u', 'l', '\0'};

Here we have declared the size of the array “name” 6, but it will only store the value up to 5 characters. last space NULL character reserves.

calculate string length without using strlen in C++

strlen is a pre-defined function in the string library, used to calculate the string length in C++, but here we will calculate string length without a using it,

in the below program, we performed two operations first display the string and second calculate the string length,

#include<iostream>
using namespace std;
int main()
{
  int i,len=0;
  char name[6]="Rahul";
  
// displaying character using loop
   cout<<"displaying string : ";
    for(i= 0; name[i]!='\0'; i++)           
      {
        cout<<name[i]; // displaying string
      }
 
//calculating string length
     for(i= 0; sec[i]!='\0'; i++)
       {
         len++; // calculate string length
       }
  
   cout<<"\n string length: "<<len++;
  
}

OUTPUT

displaying string : Rahul

string length: 6

Explanation

In the above program, we used a loop to calculate the length, here is condition “execute the loop until the variable i value does not match with 0″ , such as

you can see in the above diagram loop will be executed 5th times and in every execution, variable len will be increased by 1 which will be string size, In the 6th variable i value is match with 0 then loop will be terminated and variable len will be printed.

similarly in Two-dimensional array, 

char name[3][4];
char name[2][3]={ "one", "two", "three", "four", "five"};

as we can see in above program, It is a bit difficult to do a relative task with string, so there is already a defined library function to perform string related task in C++.

Here is an example of Character array where we get the ASCII code of name which is given by the user,

Find out the ASCII code of a string given by the user using Character array in C++

#include<iostream>
using namespace std;
int main()
{
  char n[30];

   cout<<"Enter your name: ";
   cin>>n;

   for(int i=0;n[i]!='\0';i++)
    {
      cout<<n[i]<<"\t";   // print name
    }
 
  cout<<endl;

   for(int j=0;n[j]!='\0';j++)
    {
      cout<<int(n[j])<<"\t";  // print ascii value
    }

return 0;
}

OUTPUT

1st Execution

Enter your name: rohit
r	o	h	i	t
114	111	104	105	116

2nd Execution

Enter your name: ROHIT
R	O	H	I	T	
82	79	72	73	84

Explanation

Here program executed twice, so you can understand ASCII code will different, doesn’t matter that entered string is same.

: C++ has a predefined function toascii(int); to get ASCII value of a value.

For more about ASCII code  Print all ASCII values in C++

Found out a character array size in C++

#include<iostream>
using namespace std;
 
 int main()
  {
   char name[]= "Rahul"; //{'R','a','h','u','l','\0'};
  
     cout<<"\nsize of name : "<<sizeof(float)<<" bytes";
 
return 0;
  }

OUTPUT

size of name : 6 bytes

Explanation

as we know char data-type is reserve the 1 byte in the memory but in the above Program, variable name reserves the 6 bytes because 1 byte for each character (5-bytes) and we already mentioned a character array always terminated by a NULL character so 1-byte for it. such as,Note : Character array also called C-string.


Previous – Array in C++
Next-  Structure in C++


Like it?

Leave a Reply

Your email address will not be published.

Exit mobile version