Wednesday 25 November 2015

Program to take string input from user and print it on the screen without built-in functions

//Program to take string input from user and print it on the screen without built-in functions

#include<stdio.h>

#include<conio.h>


void main()

{

      int n; // for string size

      char str[50]; // declares an character array and allocates memory to it

      int i; // integer variable used for looping

// Memory allocated is depended on the type of compiler


      clrscr(); // clearing the console screen (part of 'conio.h' header file)


      printf("Enter number of characters to be entered: ");

      scanf("%d",&n);

      printf("Enter input string: "); // asks for a string value

      for(i=0; i<=n; i++) // loop to take input step by step

      {

            scanf("%c",&str[i]); //stores value at the respective index

      } //printing the value stored in the respective character array


      printf("\nInputted string is:" ); // \n is used for printing from a new line

      for(i=0; i<=n; i++) // loop to take input step by step

      {

            printf("%c",str[i]); //stores value at the respective index

      }

      getch(); // for holding the output screen

}

No comments:

Post a Comment