Inputting string in C programming language

INPUTTING STRING IN C PROGRAMMING LANGUAGE.

There are various techniques for inputting string .
1. scanf()
2. gets()
3. getchar()

1. INPUTTING STRING USING scanf( ) :

The scanf( ) function with %s format specification can be used to read as string from the user and store it in a character string. Now let us consider a program that demonstrates how to input and display string.

Program to input and display a string entered by  the user?
#include<stdio.h>
#include<conio.h>
main( )
{
char name[20];
clrscr();
printf("Enter the name of person : ");
scanf("%s", name);
printf("Name you entered was : %s" name );
getch( );
}

OUTPUT:
Enter the name of person :  Gopal
Name you entered was : Gopal

EXPLANATION: The above program inputs and displays the name of a person. The statement,
   scanf("%s", name);
prompts the user to enter the name. The characters entered by the user are read in by the scanf( ) and is stored inside the array name in the order specified, until it finds a whitespace character(blank, tab, carriage return, form feed or newline). When it finds a trailing whitespace character, it ends the string with a null character. One should note that unlike previous scanf( ) calls, while reading strings, ampersand (&) sign is not required before the array name. This is because name is an array name so it is treated as a pointer automatically. The user should make sure the name should note the exceed 19 characters as scanf( ) automatically terminates the string with a null character and therefore the last character is reserved for  '\0' . Note that %s specification is used for inputting a string. This is then printed using the statement,
  printf("\nName entered  was : %s" ,name);
We can specify the field width using the form %ws in the scanf ( ) statement. Here, w specifies the number of character read from the input string. For example , in the following scanf() call ,
     scanf("%19s", name);
only 19 characters will be read and stored into name. We have specified 19 characters instead of 20 because you still need to leave room for the null character '\0'  .


INPUTTING STRING USING gets( )  :

In the previous program, if you enter name such as " Gopal sharma " instead of "Gopal"  then only "Gopal"  is printed when printf( ) is encountered. This is because when string are entered using scanf( ), it terminates its input when it finds the first white scape character or exceeds its specified  range and ignores the subsequent characters. So you cannot enter strings separated bu white spaces characters using scanf( ) function.
  To overcome the inability of scanf( ) to input multi word strings, C provides us a gets ( ) function that helps us to input multi word string. This function does not terminate the string input on encountering  a whitespace character. It terminates when it encounters a newline character. The gets( ) function discards the newline character insted of storing it in the array, rather null character '\0' takes place of the newline character. If we know rewrite the previous program using gets( ) it would look like ,

Program to input string using gets( ) and then display it?
#include<stdio.h>
#include<conio.h>
main( )
{
char name[20];
clrscr();
printf("Enter the name of person : ");
gets(name);
printf("Name you entered was : %s" name );
getch( );
}

OUTPUT:
Enter the name of person :  Gopal sharma
Name you entered was : Gopal sharma

EXPLANATION: It is clear from the output that using gets() we are able to input multi word strings. Actually gets( ) is a library function so we do not include its definition. The name of character array is passed as an argument in this function.


INPUTTING STRINGS USING getchar( )  :

You can also input a string using getchar( ) function. As we already know, getchar( ) function is used to input a single character from a keyboard . So to input a string using this function, you repeatedly call the getchar( ) function to read successive single characters from the input and place them into the character array.  The entire line of text can be read and stored in an array. The reading is terminated when a newline character is enclosed  and null character is then inserted at the end of the string.

Program to read a line of text upto maximum 30 characters string using getchar( ) and then display it ?
#include<stdio.h>
#include<conio.h>
main( )
{
char name[30],ch;
int count=0;
clrscr();
printf("Enter the name of person : ");
do
{
ch= getchar( );  /*Input a single character from keyboard*/
name[count]=ch;   /*Each character stored at appr. index in array*/
count++;    /*Index counter incremented after storing each char*/
}
while(ch!='\n');   /*Process continues until user press ENTER*/
name[count-1]='\0';   /*Made the last character a null character*/
printf("Name you entered was : %s" name );
getch( );
}

OUTPUT:
Enter the name of person :  Gopal sharma
Name you entered was : Gopal sharma

EXPLANATION: Thus, we can see that we can enter multiple words unless we press enter, we can enter as many characters and we have specially denoted character '\0' to the last character.
Read more

            THANKS  

No comments

Powered by Blogger.