Print Subarray from a string in c

Print Subarray


Level: Easy

Given an input character array of A and start index S and end index E, write a function that prints the sub array starting from S (including S) and ending at index E (including E). The character array may contain spaces and tabs.
Note: You are given the main function. Just write the subroutine 'void printSubarray(char *a, int start, int end)'.

Input Format:
First line is the input array A s.t. 1 <= |A| <= 8192
Second line is the start index S s.t. 0 <= S <= |A|-1
Third line is the ending index E s.t. 0 <= E <= |A|-1

Output Format:
A single line containing the sub array of A from start index S to end index E. 

Source code


Sample solution (Provided by instructor) :
#include <stdio.h>

void printSubarray(char *a, int start, int end);

int main()
{
        char array[8192];
        int start;
        int end;
        char c;
 int i;

        for(i=0;i<8192;i++){
  array[i]='\0';
        }

        c = getchar();
        i = 0;
        while ( c != '\n' ) {
  array[i] = c;
  i = i+1;
  c = getchar();
        }

        scanf("%d", &start);
        scanf("%d", &end);

        printSubarray(array,start,end);
}
/*
 *-------------------------------------------------
 * Print a[start]...a[end]
 *------------------------------------------------
 */
void printSubarray(char *a, int start, int end)
{
 int i = start;
 while (i <= end){
  putchar(*(a+i));
  i = i+1;
 }
 return;
}

Labels: , , ,