Professional Documents
Culture Documents
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”;
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
len = strlen(str);
end = len - 1;
return 0;
}
Output:
Experiment no. 3.5
#include <stdio.h>
#include <string.h>
void main()
{
char s[100];
int i,n,c=0;
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