Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to accept a paragraph(till @) using scanf function.

C Code Example — String Programs

ADVERTISEMENT

C Program to accept a paragraph(till @) using scanf function.

Objective

Write a C program to accept a multi-line paragraph until a specific symbol (@) is entered.

Algorithm / Approach

  1. Declare a large character array str[150].
  2. Use scanf("%[^@]", str) to read the input.
  3. Print the paragraph.
main.c
#include<stdio.h>
int main() {
 char str[150];
 printf("Enter a Paragaraph to exit enter @:");
 scanf("%[^@]", str);
 printf("Paragraph is: %s",str);
 return 0;
}

Expected Output

Enter a Paragaraph to exit enter @: C is a general-purpose,
 procedural,
 language@
Paragraph is:
 C is a general-purpose, procedural, language

Explanation of the Program

  • Similar to reading a sentence, we use the scanset specifier %[^@].
  • This tells scanf to keep reading everything—including spaces, tabs, and even Enter key presses (newlines)—until it sees the exact @ symbol. This is highly useful for reading bulk multi-line text input in console applications.

Complexity

Time Complexity O(n)
Space Complexity O(n)
ADVERTISEMENT