Untitled
unknown
plain_text
2 years ago
1.2 kB
7
Indexable
#include <stdio.h>
#include <string.h>
// Function to find the maximum common prefix of an array of strings
// Input: arr[] - array of strings
// n - size of the array
// Output: max_common_prefix - the maximum common prefix
void maxCommonPrefix(char arr[][1000], int n, char max\_common\_prefix[]) {
int i, j;
int len = strlen(arr[0]);
// Iterate through each character of the first string
for (i = 0; i < len; i++) {
char temp = arr[0][i];
// Check if the character is present in all the strings
for (j = 1; j < n; j++) {
if (i == strlen(arr[j]) || arr[j][i] != temp) {
temp = '\0';
break;
}
}
// If the character is present in all the strings, add it to the max_common_prefix
if (temp != '\0') {
strncat(max_common_prefix, &temp, 1);
}
}
}
int main() {
int size;
scanf("%d", &size);
char names[size][1000];
// Read the names of the people
for (int i = 0; i < size; i++) {
scanf("%s", names[i]);
}
char max_common_prefix[1000];
max_common_prefix[0] = '\0';
// Find the maximum common prefix
maxCommonPrefix(names, size, max_common_prefix);
// Print the maximum common prefix
if (max_common_prefix[0] != '\0') {
printf("%s\n", max_common_prefix);
}
return 0;
}
Editor is loading...
Leave a Comment