You are on page 1of 4

Experiment no. 3.

TO PERFORM DECLARATION, INITIALIZATION AND ACCESSING OF


CHARACTER ARRAY AND ARRAY OF STRINGS

OBJECTIVE:
· To perform declaration, initialization and accessing of character array and array of strings.
· To write programs utilizing character array and array of strings.

THEORY:
A String is a sequence of characters that are treated as a single data item and terminated by a null
character '\0'. The C language does not support strings as a data type. A string is actually a one-
dimensional array of characters in C language. These are often used to create meaningful and readable
programs.

Declaration
Character Arrays are defined in much the same manner as ordinary arrays. In general terms, a character
array declaration may be expressed as:
char array_name[ size ] ;

A string can be considered 1-D array of characters, so an array of strings is a 2-D array of characters.
Just like we can create a 2-D array of int, float etc; we can also create a 2-D array of character or array
of strings. In general terms, a string array declaration may be expressed as:
char array_name[ no. of strings ][ size ] ;

Initialization
Arrays may be initialized when they are declared. The initialization data is placed in curly {} braces
following the equals sign. An array may be partially initialized, by providing fewer data items than the
size of the array. The initialization maybe expressed as:
char array_name[ size ] = { ‘value1’, ‘value2’, ‘value3’, …}; OR
char array_name[ size ] = “value”;

Similarly array of strings can be initialized as:


char array_name[ no. of strings ][ size ] = {“value1”, “value2”, “value3”, …};

Accessing
A specific element in an character array is accessed by an index. In general terms, an element of a
character array or a string in an array of strings can be accessed as:
array_name[ index ] ;

DEMONSTRATION:
Program 1: To reverse the given string.

#include <stdio.h>
#include <string.h>

void main()
{
char str[100];
int i, j, len, start, end;
Experiment no. 3.5

printf("\n Please Enter any String : ");


gets(str);

len = strlen(str);
end = len - 1;

printf("\n:: Reverse of given string ::\n");


for(i = len - 1; i >= 0; i--)
{
if(str[i] == ' ' || i == 0)
{
if(i == 0)
{
start = 0;
}
else
{
start = i + 1;
}
for(j = start; j <= end; j++)
{
printf("%c", str[j]);
}
end = i - 1;
printf(" ");
}
}

return 0;
}

Output:
Experiment no. 3.5

Program 2 : To check if given string is palindrome or not.

#include <stdio.h>
#include <string.h>
void main()
{
char s[100];
int i,n,c=0;

printf("Enter the string : ");


gets(s);
n=strlen(s);

for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
printf("string is palindrome");
else
printf("string is not palindrome");
return 0;
}

Output:

DISCUSSION:
In first program, we reversed the given string the words stored in array of strings. And in second
program we checked if the given string is palindrome or not,

CONCLUSION:
Hence, from the above experiment we are able to perform declaration, initialization and accessing of
Experiment no. 3.5

character and string array in C programming language.

You might also like