#include <stdio.h>
#include <string.h>
void sort_array_of_strings(char arr[][100], int n) {
// Bubble Sort algorithm to sort the array of strings
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (strcmp(arr[j], arr[j + 1]) > 0) {
char temp[100];
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j + 1]);
strcpy(arr[j + 1], temp);
}
}
}
}
int main() {
int n;
scanf("%d", &n);
char arr[n][100];
for (int i = 0; i < n; i++) {
scanf("%s", arr[i]);
}
sort_array_of_strings(arr, n);
for (int i = 0; i < n; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
Let's go through the solution step by step:
We include the
The sort_array_of_strings() function takes two parameters: arr (a two-dimensional character array representing the array of strings) and n (an integer representing the number of strings in the array). It sorts the array of strings in lexicographically ascending order.
Inside the sort_array_of_strings() function, we use the Bubble Sort algorithm to sort the array of strings. We have two nested for loops to compare and swap adjacent strings if they are out of order.
We use the strcmp() function from the
In the main() function, we declare an integer variable n to store the input value for the number of strings in the array.
We use the scanf() function to read the input value from the user and store it in the variable n.
We declare a two-dimensional character array arr of size n by 100 to store the array of strings.
We use a for loop to iterate from 0 to n - 1. Inside the loop, we use the scanf() function to read each string from the user and store it in the corresponding row of the arr array.
We call the sort_array_of_strings() function with the arr array and n as arguments to sort the array of strings.
We use another for loop to iterate from 0 to n - 1 and print each sorted string using the printf() function.
By providing the expected input and executing the code, you should see the array of strings sorted in lexicographically ascending order, as specified in the problem statement on HackerRank.