Write a C Program To Wrap a Text

“wrap” text at a specific number of characters in a file. For example, if you want to wrap text at 72 characters, you would insert a new-line character after every 72 characters in the file.

C Program To Wrap a Text

#include <stdio.h>
#include <string.h>
int main() {
char ptr[256];
int i, n;
/* get the input string from the user */
printf("Enter your input string:");
fgets(ptr, 256, stdin);
ptr[strlen(ptr) - 1] = '\0';

/* get the input to wrap the text */
printf("I/P to wrap the given string(0 to %d):", strlen(ptr) - 1);
scanf("%d", &n);

/* boundary check */
if (n < 0 || n > strlen(ptr) - 1) {
printf("Boundary Value Exceeded!!\n");
return 0;

/* wrapping the string to n characters */
ptr[n] = '\0';

/* print the result */
printf("After Wrapping the given Text to %d characters..\n", n);
printf("Resultant String: %s\n", ptr);

return 0;

Output of Program

Enter your input string:5
I/P to wrap the given string(0 to 0):4
Boundary Value Exceeded!!