Reading a Whole Line C++ Without String
Get this book -> Problems on Array: For Interviews and Competitive Programming
In this article, we have explained how to take cord input in C Programming Language using C lawmaking examples. Nosotros have explained unlike cases like taking 1 word, multiple words, entire line and multiple lines using different functions.
Table of contents:
- Cord input using scanf Function
1.one. Reading One Discussion
1.2. Reading Multiple Words
1.3. Reading Multiple Words to Form a Line
i.4. Reading an entire line - Using getchar
- Reading Unabridged Line using gets()
- Read One Line at a Time from a File using fgets()
- Reading Multiple Lines
Let us learn these techniques using C lawmaking examples.
Cord input using scanf Function
The input function scanf can be used with %southward format specification to read in a string of characters.
Reading One Word
For Example :
char instr[10]; scanf("%s",instr);
The trouble 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,grade feeds and new lines.
Therefore, if the following line of text is typed :
HELLO BOY
then only the string "HELLO" will be read into the array accost , since the bare space subsequently the word 'NEW' volition stop the reading of string.
The unused locations are filled with garbage.
The scanf function automatically terminates the string that is read with a null graphic symbol and therefore, the graphic symbol assortment should be large plenty to agree the input string plus the null graphic symbol. Note that different previous scanf calls, in the case of character arrays, the ampersand (&) is not required earlier the variable name.
Reading Multiple Words
If we want to read the entire line "Hello BOY", so we may employ two grapheme arrays of appropriate sizes.
char instr1[10], instr2[ten]; scanf("%southward %south", instr1,instr2);
Information technology volition assign the string "HELLO" to instr1 and "BOY" to instr2.
#include<stdio.h> #include<cord.h> int main() { char instr[100]; printf("Enter a string\north"); scanf("%s",bin); printf("String is : \n"); puts(bin); return 0; }
If we give "WELCOME TO OPENGENUS" equally input information technology volition simply read WELCOME and displays information technology as output.
To overcome this problem we can make different arrays for all the words nowadays in the string .
Reading Multiple Words to Course a Line
#include<stdio.h> #include<string.h> int main() { char instr1[100],instr2[100],instr3[100],instr4[100]; printf("Enter the cord of four words\n"); scanf("%s",instr1); scanf("%due south",instr2); scanf("%southward",instr3); scanf("%south",instr4); printf("String is : \n"); printf("%due south %s %s %south" ,instr1,instr2,instr3,instr4 ); return 0; }
It will take iv words in four different graphic symbol arrays and displays on the output screen.
Reading an entire line
Nosotros have seen that scanf with %s or %ws can read simply strings without whitespaces.
Even so, C supports a format specification known every bit the edit ready conversion code %[..] that can exist used to read a line containing a diverseness of characters, including whitespaces.
char instr[100]; scanf("%[^\north]",instr);
A like method to above:
char instr[100]; scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZ]", instr);
Using getchar
Nosotros can utilize it repeatedly to read successive single characters from the input and identify them into a character array. Thus, an entire line of text tin can exist read and stored in an array. The reading is terminated when the new line character ('\n')
is entered and the null character is then inserted at the finish of the string.
#include<stdio.h> #include<string.h> main() { char instr[100] , ch; int c = 0; printf("Enter the line \n"); do { ch = getchar(); instr[c]=ch; c++; }while(ch != '\n'); c=c-1; instr[c]='\0'; printf("%southward \n", instr); }
Reading Entire Line using gets()
Another method of reading a cord of text containing whitespaces is to use the library function gets available in the <stdio.h> header file.
Information technology reads characters from the keyboard until a new line character is encountered and and so appends a null graphic symbol to the string.
Dissimilar scanf, it does non skip whitespaces.
For example :
char instr[100]; gets (line); printf("%s", instr);
It will reads a line from the keyboard and displays it on the screen.
The last program can be besides written as :
char instr[100]; printf("%south" , gets(line));
Please notation that C does not check for array bounds , so be careful non to input more grapheme that tin can be stored in the string variable used.
A consummate program to demonstrate working of gets :
#include<stdio.h> #include<string.h> int chief() { char instr[100]; printf("Enter a string\n"); scanf("%[^\north]",instr); printf("The output is:-\n"); puts(bin); return 0; }
This program will piece of 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 Time from a File using fgets()
The function fgets() takes three parameters that is :
- string_name
- string_max_size
- stdin
Nosotros tin can write it as :
fgets(string_name,string_max_size,stdin);
For Example :
#include<stdio.h> #include<string.h> int primary() { char instr[100]; printf("Enter the string\due north"); fgets(instr,100,stdin); printf("The cord is:-\northward"); puts(bin); render 0; }
Information technology will take the unabridged string as 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\n"); scanf("%[^\r]southward",s); printf("Entered String is\northward"); printf("%southward\n",south); render 0; }
Question
Which of the following is the correct input statement for reading an unabridged string from keyboard ?
All of them are correct.
gets(str);
scanf("%[^\north]",str);
fgets(instr,100,stdin);
All these methods are discussed in the article that is gets() , scanf("%[^\n]",str); and fgets()
Question
A graphic symbol assortment instr[] stores the "HELLO". What will be the length of array instr[] requires to store the string completely ?
6
5
4
None of these
As the word hello is 5 characters long and in that location will be a null grapheme at end , nosotros crave an array of size 6.
Question
State Truthful or Faux : "The input role gets has one string parameter ."
True
False
The input function gets has one string parameter which reads characters from the keyboard until a new line graphic symbol is encountered and and then appends a null character to the string.
So, with this article at OpenGenus, we accept seen all the possible methods to accept input string in C programming linguistic communication.
delatorreacqualatithe.blogspot.com
Source: https://iq.opengenus.org/how-to-take-string-input-in-c/
0 Response to "Reading a Whole Line C++ Without String"
Post a Comment