This blog is under construction

Thursday 18 July 2013

C program to wrap a text

Write a 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:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input string:Hello world
  I/P to wrap the given string(0 to 10):5
  After Wrapping the given Text to 5 characters..
  Resultant String: Hello


No comments:

Post a Comment