In C, the char data type only stores a character. Then, how one can
declare a string variable in C? Fortunately or unfortunately, I feel,
that, the inventors of C language did not think about it when they
designed the language basics! What I myself think and feel is that,
later on they found this mistake of theirs and added the concept of
character arrays or character pointers into C.
So, string is a sequence of characters stored in a character type
array, which is terminated with a null character which is denoted
by \0 or denoted by NULL, which is predefined constant.
As a rule in C, the character array must be declared using array declaration
syntax,
char string_name[maximum-string-size];
|
The maximum number of characters in an array type declaration of string
is fixed where as the arbitrary length declaration stretches size
of string to the maximum possible length, which is determined by the
computer system’s memory configuration.
Declaring a string of arbitrary length is as follows,
char string_name[];
char *string_name;
|
In C, we can initialize a value to a string variable using the following
statements,
char string_name[maximum-string-size] = {‘c1’, ‘c2’,
’c3’,…};
char string_name[] = {‘c1’, ‘c2’, ’c3’,…};
char *string_name= "String-itself";
|
In above first two cases, c1, c2, c3 are the three characters, which
are supposed to store in the string variable string-name. While using
the pointer variable, one can directly specify the string itself,
rather than assigning values character by character.
In some cases, the string initialization also takes place in this
way.
By using the statement " string_name="string-itself";
", you can assign the string value to the pointer variable
in the middle of the program.
Now, let us look into a program, which initializes string and prints
the same.
#include<stdio.h>
void main(void)
{
char str1[6] = {‘H’, ‘E’, ‘L’,’L’,’O’};
printf("The string is %s", str1);
}
|
In the above mentioned program, we have assigned values H, E, L,
L, O character wise. This forms a string called "HELLO"
and it is printed on the screen.
Next, we will look into a program which reads a string and display
on to the screen character wise and also display’s the total
number of characters found in the string.
#include<stdio.h>
void main(void)
{
char str1[25];
int i=0;
printf("Enter a string: ");
scanf("%s", str1);
printf("\nThe string which is read from the key board
is, ");
while(str1[i] != NULL)
{
printf("%c", str1[i]);
i = i + 1;
}
printf("The characters that are found
in the given string is %d.", i);
}
|
The above program displays the characters which are found in a given
string. In the program, the variable i is mainly used for maintain
the index of array elements. Initially, value of the variable I
will be set to 0 and goes on incrementing, which is stopped when
the array element reaches NULL value. The value of index will be
used to determine the number of characters, which the string variable
has got.
By my own experience, I would like to say that, one can easily develop
their own programming logic, if they are well versed with string
and array handling independently without using the built-in functions,
for reversing, merging methodology.