STRING IN C.
STRING IN C.
STRING:
While working with programs we donot always deal with numeric data, we might sometimes need to operate on character data. For example , we might have programs to sort the list of student names, input the names of items purchased etc. In order to deal with such program we need to study about strings. Before starting with string, let us recall briefly what are character constants and what are values stored in char type variables.A character constant is a single character which might be a alphabet, a digit or a special symbol enclosed in single inverted commas. For example : 'A' , '$' , '2' etc. The character constants have integer values that is determined by the ASCII character set (by most compilers). These character constants str stored in variables of char type . For example : The statement,
char ch ='a';
informs the compiler that ch is a char type variable that holds a character constant 'a'.
STRING LITERALS :
A string literal (also known as string constant) is a sequence of zero or more characters or symbols that are enclosed in double quotes. C treats it as an array of characters terminated by a special character (\'0') known as null character which is inserted automatically by the compiler to mark the end of the string. When a C compiler encounters a string literal, it allocates memory to store the characters of the string plus for one extra character '\0' .Some other valid string literals are :
"good morning"
"welcome to the world of programming"
"0183-252270"
"b"
DECLARING STRING VARIABLES:
To store a string in a variable , we use an array of characters. It takes the following syntax,char string_variable[size];
The string_variable is actually the name of the character array and size represents the maximum number of characters that can be stored and it must be a positive integer value. since string are always terminated by a null character so to store a string upto n characters, the size of the array should be n+1. For example , consider the statement,
char name [30];
here char is data type , name is name of the arrays of characters and [30] is size specification. The above statement defines an array and reserves 30 bytes for storing characters.
INITIALIZING STRINGS:
Strings are basically arrays that can be initialized at the time of declaration. The following example shows different ways of initializing strings.char name[6[= "GOPAL" ;
char name[6]= {'G', 'O', 'P', 'A, 'L', '\0'} ;
char name[ ]= "GOPAL" ;
In the method of initialization, the compiler allocates space for 6 characters for array name. The first five memory locations are filled with 'G', 'O', 'P', 'A', 'L' Characters and the last with character '\0' (null character) automatically. In particular, the first character 'G' is stored at name[0], character 'O' is stored at name[1] and so on the last character '\0' is stored at name[5].
Now let us consider the following program to explain initialization,
Program that initializes strings and displays them?
#include<stdio.h>
#include<conio.h>
main()
{
char name1[] = "pankaj";
char name2[] = "gopal";
char name3[] = {'a', 'n', 'k', 'u', 'r', '\0');
clrscr();
printf("various strings are ------\n");
printf("%s\n", name1);
printf("%s\n", name 2);
puts (name 3);
getch();
}
OUTPUT:
various strings are ------
pankaj
gopal
ankur
TRAVERSING STRINGS:
An operation in which individual character of a string is handled one by one is known as traversing a string. In order to traverse a string, we use loops and especially for loop as they are relatively easy to handle.The main logic is that as string end with '\0', so we keep on traversing until the character stored at a particular index position is not '\0' . When '\0' is encountered it means that end of string is reached. Now let us consider the following program segment,
#include<stdio.h>
#include<conio.h>
main()
{
char str[] = "pankaj";
int i;
clrscr();
printf("the string is = ");
for(i=0; str[i]!= '0';i++)
printf("%c" , str[i]);
grtch();
}
OUTPUT:
the string is = pankaj
PASSING STRING TO FUNCTIONS:
Since C treats as character arrays so the rules for passing string to function are similiar as that of passing arrays to function.To pass an array of characters(i.e. string) to a function, the array name must be specified without any brackets within the function call . Array are passed by reference so any changes made to the original array in the function are reflected back in the calling function. As string are terminated bu null character, so unlike integer arrays, we generally do not pass the number of elements along with the array name.
The following program show how string are passed toa function.
To print the ASCII equivalent of each character in the given string?
#include<stdio.h>
#include<conio.h>
void string_ascii(char []); /*function prototype*/
main()
{
char str[]="good";
int i;
clrscr();
printf("characters of \"%s\" has ASCII values as follows --",str);
string_ascii(str); /*function call */
getch();
}
void string_ascii(char s[]) /*function header*/
{
int i;
for(i=0; s[i]!='\0';i++)
printf("\nASCII value of %c = %d ",s[i], s[i]);
}
OUTPUT:
characters of "good" has ASCII values as follows --
ASCII value of g=71
ASCII value of o=111
ASCII value of o=111
ASCII value of d=100
STRING INPUT / OUTPUT:
C provides a varity of functions for inputting and outputting strings.OUTPUTTING STRING:
String can be outputted using library function printf(), puts() and putchar(). We have already used printf() in our programs. Using puts() library function which is included in stdio.h header file, we can display the contents of the string variable.The puts() function takes the following form,
puts (str);
Here, str is a string variable containing a string literal. It prints the value of the string variable str and then moves the cursor to the beginning of the next line on the screen.
The putchar() function is used to output the values of the character variables. To print a string of characters using putchar() function, we use this function repeatedly to output a string of characters stored in a character array using a loop.
We have discussed techniques for inputting string using scanf (), gets() and getchar() in next page.
Read more
THANKS
Leave a Comment