C Programs Using Array of Strings

Here are some C programs that demonstrate the usage of arrays of strings (array of character arrays):

1. Program to Input and Print an Array of Strings:


#include <stdio.h>

int main() {
char strings[3][50];

printf("Enter three strings:\n");

for (int i = 0; i < 3; i++) {
	printf("String %d: ", i + 1);
	gets(strings[i]);
}

printf("You entered:\n");
for (int i = 0; i < 3; i++) {
	printf("String %d: %s\n", i + 1, strings[i]);
}

return 0;
}

This program allows the user to input three strings into an array and then prints the array of strings.

For this program, when you run it and provide input, the output will depend on the strings you enter. Here's an example of what the output might look like if you enter "Hello," for the first string, "World!" for the second string, and "C Programming" for the third string:


Enter three strings:
String 1: Hello,
String 2: World!
String 3: C Programming
You entered:
String 1: Hello,
String 2: World!
String 3: C Programming

2. Program to Find the Longest String in an Array:

#include <stdio.h>
#include <string.h>

int main() {
char strings[4][50];
char longest[50];

printf("Enter four strings:\n");

for (int i = 0; i < 4; i++) {
	printf("String %d: ", i + 1);
	gets(strings[i]);
}

strcpy(longest, strings[0]);

for (int i = 1; i < 4; i++) {
	if (strlen(strings[i]) > strlen(longest)) {
		strcpy(longest, strings[i]);
	}
}

printf("The longest string is: %s\n", longest);

return 0;
}

This program allows the user to input four strings into an array and then finds and prints the longest string.

For this program, if you enter various strings, it will find and print the longest string. Here's an example:


Enter four strings:
String 1: Programming
String 2: is
String 3: fun
String 4: in C

The longest string is: Programming

3. Program to Sort an Array of Strings in Lexicographical Order:

#include <stdio.h>
#include <string.h>

int main() {
char strings[5][50];

printf("Enter five strings:\n");

for (int i = 0; i < 5; i++) {
	printf("String %d: ", i + 1);
	gets(strings[i]);
}

// Sort the array of strings
for (int i = 0; i < 5; i++) {
	for (int j = i + 1; j < 5; j++) {
		if (strcmp(strings[i], strings[j]) > 0) {
			char temp[50];
			strcpy(temp, strings[i]);
			strcpy(strings[i], strings[j]);
			strcpy(strings[j], temp);
		}
	}
}

printf("Sorted array of strings:\n");
for (int i = 0; i < 5; i++) {
	printf("String %d: %s\n", i + 1, strings[i]);
}

return 0;
}

This program allows the user to input five strings into an array and then sorts and prints the array of strings in lexicographical order.

For this program, when you enter five strings, it will sort them in lexicographical (alphabetical) order and then print the sorted array. Here's an example:


Enter five strings:
String 1: Orange
String 2: Banana
String 3: Apple
String 4: Grape
String 5: Kiwi

Sorted array of strings:
String 1: Apple
String 2: Banana
String 3: Grape
String 4: Kiwi
String 5: Orange

The exact output will depend on the input provided when you run the programs.

These programs demonstrate various operations and tasks that can be performed using arrays of strings in C. You can use these as examples and modify them according to your specific requirements.