C Language



Admission Enquiry Form

  

Character Array in C


What is Character Array in C ?

or

What is String in C ?

A character array is also called as strings in C programming can store more than one characters and are terminated by null character i.e. '\0' at the end by the compiler automatically.
Character array can only be of type char.




Syntax to declare Character Array

data_type  arrayName[size];

This is called a character array. The arraySize must be an integer constant greater than zero and type will only be char in C. For example, to declare a character array with size 10 name variable of type char, use this statement:

char name[10];

Initialisation of charcter array or a string

We can initialize character in different ways.Here, we are going to explain this with an example.Following is the example in which we will declare it with variable name and initialize it with name "compuhelp".

1. char name[10]={'c','o','m','p','u','h','e','l','p'};
2. char name[]={'c','o','m','p','u','h','e','l','p'};
3. char name[10]="compuhelp";
4. char name[]="compuhelp";



Example 1

Output

Enter namecompu help

Name is compu

In the above ,you can see that array variable is name and size is 11.Here, we are taking the input from the user and using %s as format specifier which can be used to directly print and read strings.
User enters compu help.But you can see the result, that there is only compu not help is printed. This is due to the use of scanf(). scanf() does not read spaces.For that now we will use another function to read the string with spaces and that is gets(). puts() is also there to display the string.




Example 2

Output

Enter namecompu help
compu help



Example 3

Output

compuhelp



Use of Character in C

//use of character in c
#include<stdio.h>
int main()
{
char ch='a';
printf("\n%c",ch);//it will display a
printf("\n%d",ch);//it will display ASCII value
ch=ch-32;//it will change into uppercase ASCII value
printf("\n %c",ch);//it will display A
return 0;
}




Use of Character Array in C

Code to Find the Length of a String

//Code to find the length of a string:
#include<stdio.h>
int main()
{
     int i;
     char str[10];
     printf("Enter string:");
     scanf("%s",str);
     printf("\n your string is %s",str);

//code to find length of string
for(i=0;i<10;i++)
{
     if(str[i]=='\0')
     {
          printf("\n length of string is %d",i);
          break;
     }
}//end of for loop
}//end of main




Code to Reverse a String

//Code to reverse a string:
#include<stdio.h>
int main()
{
     int i;
     char str[10];
     printf("Enter string:");
     scanf("%s",str);
     printf("\n your string is %s",str);

//code to find length of string
for(i=0;i<10;i++)
{
     if(str[i]=='\0')
     {
          printf("\n length of string is %d",i);
          break;
     }
}//end of for loop
printf("\n String in reverse order:\n");
//code to display string in reverse order:
for(i=i-1;i>=0;i--)
{
     printf("%c",str[i]);
}
}//end of main