Handling of Character Strings: Declaring and Initializing String Variables
Handling of Character Strings: Declaring and Initializing String Variables
Handling of Character Strings: Declaring and Initializing String Variables
A string is nothing but a group of characters. The group of characters will be treated as
arrays in C-language. Any group of characters defined within double quotes is treated
as strings.
Example: “I am learning about strings in C-language”
Till now whatever you provided in printf statements are character strings only, they
provided some meaningful and readable information to the user.
char string-name[size];
the size in a character string represents the number of characters that a character
string can hold. Each character of the string can be treated as an element of the array
name and is stored in the memory. For example if you assign a string “NEW DELHI”,
then it will be stored as follows
‘N’
‘E’
‘W’
‘‘
‘D’
‘E’
‘L’
‘H’
‘I’
‘\0’
When the compiler recognizes the variable as string variable it terminates it with an
additional character ‘\0’ at the end.
while reading the sting data the ‘&’ sign is not required before the variable name
because a character string is always represents a pointer to that string.
Example2:
main()
{
char name[10];
printf(“enter a name:”)
scanf(“%s”,name);
printf(“ the name=%s”,name);
}
scanf(“[^\n]s”, variable);
Example3:
main()
{
char str1[10],str2[10];
printf(“enter a name without spaces :”)
scanf(“%s”,str1);
printf(“ the name=%s”,str1);
printf(“enter a name with spaces :”)
scanf(“%s”,str1);
scanf(“%s”,str2);
printf(“ the name in str1=%s”,str1);
printf(“ the name in str2=%s”str2
}
Example4:
main()
{
char str1[10],str2[10];
printf(“enter a name without spaces :”)
scanf(“%s”,str1);
printf(“ the name=%s”,str1);
printf(“enter a name with spaces :”)
scanf(“%[^\n]s”,str2);
printf(“ the name in str2=%s”str2
}
NOTE: The type specifier “[^\n]s “ can be applicable only when we use scanf() it is not
valid for printf(). It directs the compiler to accept the string till ‘ \n ‘ encounters.
main()
{
int i;
char name[10];
printf(“enter a name:”)
scanf(“%s”,name);
for(i=0;name[i]!=’\0’;i++)
printf(“ the character in name[%d] =%c\n”,name[i]);
}
Example 5:
Write a C-program to find the length of a string
main()
{
char string[10];
int i;
printf(“enter a string:”);
scanf(“%d”, string);
for(i=0;string[i]!=’\0’;i++);
printf(“the length of string is %d”, i);
}
There is function in string.h header file which helps to find the length of string i.e.
strlen().
NOTE: While copying of strings the source length should be equal or more than that of
the destination string so as to accommodate the length of both the strings.
programing
programin
programi
program
progra
progr
prog
pro
pr
p
Example7:
#include<string.h>
#include<stdio.h>
main()
{
static char str[12]="programing";
int i,len,w;
clrscr();
len=strlen(str);
for(i=0,w=len;str[i]!='\0';i++,len--)
printf("%-*.*s\n",w,len,str);
getch();
}
In the above program first * in type-specifier is for width and second * for precision. We
can also specify the integer constants instead of * like,
printf(“%-12.*s\n”,len,str);
Two strings cannot be added directly using addition operator(+) the statement such as
string3 =string1+ string2 is not valid the process of adding two strings is known as
concatenating. The following program explains the logic for concatenation.
Example10:
NOTE: While catenation of strings the source string (string1) length should be double or
more than that of the destination string(string2) so as to accommodate the addition
length of both the strings.
Example for the usage of strcat():
main()
{
char str1[20],str2[10];
printf(“ The string str1 after concat with a white space = %s* ”, str1);
strcat(str1,str2);
printf(“ The string str1 after concat with str2 = %s”, str1);
}
Output:
enter first string less than 10 characters: John
enter first string less than 10 characters: Travolta
The string str1 after concat with a white space = John *
The string str1 after concat with str2 = John Travolta
main()
{
char str1[10],str2[10]
int i;
printf(“enter first string);
scanf(“%s”,str1);
printf(“enter second string);
scanf(“%s”,str2);
i=0;
while((str1[i++]==str2[i]) && str1[i]!=’\0’ && str2[i] !=’\0’)
i++;
if(str1[i]==’\0’ && str2[i]==’\0’)
printf(“Both are Equal”);
else
printf(“Both are not Equal”);
}
There is function in string.h header file which helps to compares two strings i.e.
strcmp(). This function returns an integer value, which is the difference between the
ASCII values of differed characters of two strings.
If the differences between characters of first string and second string is that of positive,
then the first string is greater than second one.
If the differences between characters of first string and second string is that of negative,
then the first string is less than second one.
If the difference between characters of first string and second string is that of zero, then
both the strings are equal.
The general syntax for this function is strcmp(string1,string2)
Example for strcmp()
main()
{
char str1[10],str2[10]
int i;
printf(“enter first string);
scanf(“%s”,str1);
printf(“enter second string);
scanf(“%s”,str2);
if(strcmp(str1,str2)==0)
printf(“Both are Equal”);
else if(strcmp(str1,str2)>0)
printf(“str1 is greater than str2”);
else
printf(“str1 is less than str2”);
}
Example:
main()
{
char line[50],ch;
int i=0;
printf(“enter the line of text and press return at the end:”);
do
{
ch = getchar();
line[i]=ch;
i++;
}while(ch!=’\0’);
i - -;
line[i]=’\0’;
printf(“the entered line is = %s”, line);
}
Two-dimensional string arrays:
A list of names is treated as a table of strings. Whenever it is necessary to store multiple
names with only one variable name, it is necessary to declare the variable as two-
dimensional array. The declaration is like an integer array.
Data-Type variable-name[index1][index2]
In the above syntax index1 indicates the number of students and index2 indicates the
length of each string. For example if the variable is declared as name[5][20] then it
indicates that the variable name can be able to hold 5 names with a length of 20
characters each.
To access the name of city at ith location access that with its index value i.e. if want to
access the city HYDERABAD in the array then access it with its index city [2]. Once an
array is declared as two-dimensional array, it can be accessed like a one-dimensional
array in further manipulations.