1.
#include
int main() {
char para[100];
printf("Enter Paragraph : ");
scanf("%[^\t]s",para);
printf("Accepted Paragraph : %s", para);
return 0;
}
2.
#include
int main() {
char para[100];
printf("Enter Paragraph : ");
scanf("%20[^\t]s",para);
printf("Accepted Paragraph : %s", para);
return 0;
}
#include
int main() {
char para[100];
printf("Enter Paragraph : ");
scanf("%[^\t]s",para);
printf("Accepted Paragraph : %s", para);
return 0;
}
- Here scanf will accept Characters entered with spaces.
- It also accepts the Words, newline characters.
- %[^\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 :
- Paragraph Size cannot be estimated at Compile Time
- It’s vulnerable to buffer overflows.
How to Specify Maximum Size to Avoid Overflow?
//------------------------------------
// Accepts only 10 Characters
//------------------------------------
scanf("%10[^\t]s", para);
#include
int main() {
char para[100];
printf("Enter Paragraph : ");
scanf("%20[^\t]s",para);
printf("Accepted Paragraph : %s", para);
return 0;
}
Comments
Post a Comment