Problem: Write a C++ program to collect a positive integer n from the user, and then collect n character strings from the user (containing only lower case alphabets and no other characters, not even spaces). The strings need not be of equal length but the maximum length of any string should be 11. The strings should be stored in an appropriate 2D array. Your program should then include the following:

A function string_compare which takes two strings as input and returns 1 if the first string appears before the second string in alphabetical order, -1 if the second string appears before the first in alphabetical order, and 0 if the two strings are equal to each other. Do not use the inbuilt strcmp function.
A function string_sort which sorts the character strings using the mergesort algorithm taught in class. You are recommended to write a recursive implementation. You will automatically lose 50% of the marks for this program if you implemented the sorting within main.
Your program should then print out the strings in sorted alphabetical order, starting from the first to the last. Feel free to refer to the lecture slides for help.

Input Format
integer n, and a set of n character strings


Output Format
Print out the strings in the suggested order, one string on a new line


Constraints
n is a positive integer, n <= 15
For ease of display of the output, the maximum length of any of the strings should be 11, excluding the NULL character
The character strings should contain only lower case alphabets


Sample input and output




Input

Output

4

zebra

lion

tiger

monkey



lion

monkey

tiger

zebra







Note:  

Do not write any C++ statements for printing general messages. For example, the following should NOT be present in your program: 
cout << "Enter a number:",  
cout << "The computed answer is", etc.  


cout should be used to print only the computed final output. In addition, do not print unnecessary spaces unless specified in the program.

Student submission:
#include<iostream>
using namespace std;

int string_compare(char* str1, char* str2){
	int i=0, str1len = 0, str2len  = 0;

	while(str1[i] != 0){
		str1len++; i++;
	}
	i=0;
	while(str2[i] != 0){
		str2len++; i++;
	}
	i=0;
	while((i<str1len)&&(i<str2len)){
		if(str1[i]<str2[i]){return 1;}
		else if(str1[i]>str2[i]){return -1;}
		i++;
	}
	return 0;
}

void string_merge(char a[][12], int low, int mid, int high){
	

	char b1[mid-low+1][12], b2[high-mid][12];
	for(int i=0; i <mid-low+1; i++){for(int k=0; k<12; k++){b1[i][k] = a[low+i][k];}}
	for(int i=0; i <high-mid; i++){for(int k=0; k<12; k++){b2[i][k] = a[mid+i+1][k];}}

	int i=0, j=0, c=0;
	while((i<mid-low+1)&&(j<high-mid)){
		if((string_compare(b1[i], b2[j])==1)||(string_compare(b1[i], b2[j])==0)){
			for(int k=0; k<12; k++){a[low+c][k]=b1[i][k];}
				i++;
		}
		else if(string_compare(b1[i], b2[j])==-1){
			for(int k=0; k<12; k++){a[low+c][k]=b2[j][k];}
				j++;
		}
		c++;
	}

	while(i<mid-low+1){
		for(int k=0; k<12; k++){a[low+c][k]=b1[i][k];}
		i++;
		c++;
	}
	while(i<mid-low+1){
		for(int k=0; k<12; k++){a[low+c][k]=b2[j][k];}
		j++;
		c++;
	}
}

void string_sort(char arr[][12], int low, int high){
	if((high-low)==0){return;}
	else{
		int mid = (low+high)/2;
		string_sort(arr, low, mid);
		string_sort(arr, mid+1, high);

		string_merge(arr, low, mid, high);
	}
}

int main(){
	int n;
	cin >> n;
	char arr[n][12];
	for(int i=0; i<n; i++){cin >> arr[i];}
	
	string_sort(arr, 0, n-1);

	for(int i=0; i<n-1; i++){
		cout << arr[i] << endl;
	}
	cout << arr[n-1];
}

Grading criterion: Quality and number of Comments
Rating A: Well Commented and Formatted
Rating B: Ok Commented or Ok Formatted
Rating C: Poor Comments and Formatting
Rating D: 0
Assigned grade: B