How to Make Getchar Read One Word

Internship at OpenGenus

Get this volume -> Problems on Assortment: For Interviews and Competitive Programming

In this article, we have explained how to take string input in C Programming Language using C code examples. We have explained unlike cases like taking one give-and-take, multiple words, entire line and multiple lines using different functions.

Table of contents:

  1. Cord input using scanf Office
    1.one. Reading 1 Word
    1.2. Reading Multiple Words
    1.3. Reading Multiple Words to Class a Line
    1.4. Reading an entire line
  2. Using getchar
  3. Reading Entire Line using gets()
  4. Read One Line at a Time from a File using fgets()
  5. Reading Multiple Lines

Let us learn these techniques using C code examples.

Cord input using scanf Function

The input office scanf can exist used with %s format specification to read in a string of characters.

Reading One Word

For Example :

              char instr[10]; scanf("%s",instr);                          

The problem with the scanf function is that it terminates its input on the first white space information technology finds. A white space includes blanks,tabs,carraige returns,form feeds and new lines.

Therefore, if the following line of text is typed :

              HELLO BOY                          

and then simply the string "HELLO" will be read into the array accost , since the blank space later the word 'NEW' will terminate the reading of cord.
The unused locations are filled with garbage.

The scanf function automatically terminates the cord that is read with a zilch character and therefore, the grapheme array should exist large plenty to concur the input cord plus the null character. Note that unlike previous scanf calls, in the case of graphic symbol arrays, the ampersand (&) is non required before the variable proper noun.

Reading Multiple Words

If nosotros want to read the entire line "Hi Male child", then nosotros may utilise two graphic symbol arrays of advisable sizes.

              char instr1[10], instr2[10]; scanf("%s %southward", instr1,instr2);                          

It volition assign the string "Howdy" to instr1 and "BOY" to instr2.

              #include<stdio.h> #include<cord.h> int master() {     char instr[100];        printf("Enter a string\n");     scanf("%s",bin);     printf("Cord is : \n");     puts(bin);          return 0; }                          

If we give "WELCOME TO OPENGENUS" as input it will only read WELCOME and displays it as output.

To overcome this problem we tin can make different arrays for all the words present in the string .

Reading Multiple Words to Course a Line

              #include<stdio.h> #include<cord.h> int main() {     char instr1[100],instr2[100],instr3[100],instr4[100];          printf("Enter the string of iv words\n");     scanf("%s",instr1);     scanf("%s",instr2);     scanf("%s",instr3);     scanf("%s",instr4);     printf("String is : \northward");          printf("%s %s %s %s" ,instr1,instr2,instr3,instr4 );          return 0; }                          

It volition have 4 words in 4 dissimilar grapheme arrays and displays on the output screen.

Reading an entire line

We have seen that scanf with %due south or %ws can read simply strings without whitespaces.
However, C supports a format specification known as the edit gear up conversion code %[..] that can exist used to read a line containing a variety of characters, including whitespaces.

              char instr[100]; scanf("%[^\north]",instr);                          

A similar method to above:

              char instr[100]; scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZ]", instr);                          

Using getchar

We can use it repeatedly to read successive single characters from the input and identify them into a grapheme array. Thus, an entire line of text can exist read and stored in an array. The reading is terminated when the new line graphic symbol ('\due north')
is entered and the nix character is then inserted at the end of the cord.

              #include<stdio.h> #include<string.h> principal() {     char instr[100] , ch;     int c = 0;     printf("Enter the line \n");     do     {          ch = getchar();          instr[c]=ch;          c++;     }while(ch != '\northward');      c=c-1;     instr[c]='\0';     printf("%s \north", instr); }                          

Reading Entire Line using gets()

Some other method of reading a string of text containing whitespaces is to use the library part gets available in the <stdio.h> header file.
It reads characters from the keyboard until a new line grapheme is encountered and and so appends a null character to the string.
Unlike scanf, it does not skip whitespaces.

For case :

              char instr[100]; gets (line); printf("%s", instr);                          

It will reads a line from the keyboard and displays it on the screen.

The last plan can be also written as :

              char instr[100]; printf("%south" , gets(line));                          

Please note that C does not bank check for array bounds , and so exist careful not to input more graphic symbol that can be stored in the string variable used.

A complete plan to demonstrate working of gets :

              #include<stdio.h> #include<cord.h> int principal() {     char instr[100];          printf("Enter a string\n");     scanf("%[^\n]",instr);     printf("The output is:-\n");     puts(bin);          return 0; }                          

This program will work perfectly fine and it will take the unabridged string as input from the keyboard and displays it on the output screen.

Read One Line at a Fourth dimension from a File using fgets()

The role fgets() takes iii parameters that is :

  1. string_name
  2. string_max_size
  3. stdin

Nosotros tin can write it as :

              fgets(string_name,string_max_size,stdin);                          

For Example :

              #include<stdio.h> #include<string.h> int main() {     char instr[100];          printf("Enter the string\northward");     fgets(instr,100,stdin);     printf("The string is:-\n");     puts(bin);          return 0; }                          

It volition take the entire string every bit input from the keyboard and displays the unabridged screen on the output screen.

Reading Multiple Lines

The following program reads multiple line from the keyboard.

              #include<stdio.h> int main() {     int s[100];     printf("Enter multiple line strings\due north");     scanf("%[^\r]s",s);     printf("Entered String is\n");     printf("%due south\n",southward);     return 0; }                          

Question

Which of the following is the correct input statement for reading an entire cord from keyboard ?

All of them are correct.

gets(str);

scanf("%[^\n]",str);

fgets(instr,100,stdin);

All these methods are discussed in the article that is gets() , scanf("%[^\n]",str); and fgets()

Question

A grapheme array instr[] stores the "Hi". What volition be the length of array instr[] requires to shop the cord completely ?

6

5

iv

None of these

Every bit the discussion hello is 5 characters long and at that place will exist a null character at end , we crave an array of size 6.

Question

State True or Imitation : "The input function gets has ane string parameter ."

True

Imitation

The input role gets has 1 string parameter which reads characters from the keyboard until a new line character is encountered so appends a nil character to the string.

So, with this article at OpenGenus, we have seen all the possible methods to accept input string in C programming language.

lovewelesepind.blogspot.com

Source: https://iq.opengenus.org/how-to-take-string-input-in-c/

0 Response to "How to Make Getchar Read One Word"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel