C Program to Accept Paragraph using scanf

1.
#include

int main() {
   char para[100];

   printf("Enter Paragraph : ");
scanf("%[^\t]s",para);
   printf("Accepted Paragraph : %s", para);

   return 0;
}
  1. Here scanf will accept Characters entered with spaces.
  2. It also accepts the Words, newline characters.
  3. %[^\t]s  represent that all characters are accepted except tab(t), whenever t will be 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?
//------------------------------------
// Accepts only 10 Characters
//------------------------------------
scanf("%10[^\t]s", para);


2.

#include

int main() {
   char para[100];

   printf("Enter Paragraph : ");
scanf("%20[^\t]s",para);
   printf("Accepted Paragraph : %s", para);

   return 0;
}

Comments