In this problem, we are given an array of string. Our task is to create a c program to reverse array of strings.
We will reverse the array elements i.e. last element to the first value and so on.
Let’s take an example to understand the problem,
Input
strarr[] = {"learn", "programming", "at", "tutorialspoint"}
Output
strarr[] = {"tutorialspoint", "at", "programming", "learn"}
To solve this problem, we will create an array of pointers and use two pointers from start and end. then move the pointers towards the opposite side, and keep on swapping the pointer values.
C program to reverse array of strings.
//c program to reverse array of strings.
Example
#include <stdio.h> #include <string.h> void ReverseStringArray(char* strarr[], int n) { char* temp; int end = n - 1; for (int start = 0; start < end; start++) { temp = strarr[start]; strarr[start] = strarr[end]; strarr[end] = temp; end--; } } int main() { char* strarr[] = {"learn", "programming", "at", "tutorialspoint"}; int n = sizeof(strarr) / sizeof(strarr[0]); for (int i = 0; i < n; i++) printf("%s ", strarr[i]); printf("\n"); ReverseStringArray(strarr, n); for (int i = 0; i < n; i++) printf("%s ", strarr[i]); return 0; }
Output
learn programming at tutorialspoint tutorialspoint at programming learn