Monday, 25 August 2014

C Program to Accept Paragraph using scanf

C Program to Accept Paragraph using scanf: I f  you think that scanf does not accept paragraph then your are wrong,because here we learn a way from which we can insert paragraph with space.

C Program to Accept Paragraph using scanf


C Program to Accept Paragraph using scanf

 
 #include<stdio.h>
 
int main() {
   char pgraph[400];
 
   printf("Enter Paragraph : ");
   scanf("%[^\t]s", pgraph);
 
   printf("Accepted Paragraph : %s", pgraph);
 
   return 0;
}

Note: Press Tab to Stop Accepting Characters

Output :
 Enter Paragraph :Geek Coderz is a blog which covers the stuff related to progr amming languages and tutorials. 
Accepted Paragraph : Geek Coderz is a blog which covers the stuff related to pr ogramming languages and tutorials. 


Explanation :


    scanf("%[^\t]s", pgraph);

  1. Here scanf will accept Characters entered with spaces.
  2. It also accepts the Words , new line characters .
  3. %[^\t]s  represent that all characters are accepted except tab(t) whenever t will encountered then the process of accepting characters will be terminated.
Drawbacks :
  1. Paragraph Size cannot be estimated at Compile Time
  2. It’s vulnerable to buffer overflows.
How to Specify Maximum Size to Avoid Overflow ?
// Accept only 100 character
 scanf("%10[^\t]s", pgraph);