C语言实现查找字符串数组中的最长公共前缀
时间: 2023-09-13 19:07:34 浏览: 150
C语言求两个字符串的最长公共子串
5星 · 资源好评率100%
```c
#include <stdio.h>
#include <string.h>
int main()
{
char strs[100][100] = {"flower", "flow", "flight"};
int n = 3, i, j;
char prefix[100] = "";
for (i = 0; strs[0][i] != '\0'; i++) {
for (j = 1; j < n; j++) {
if (strs[j][i] != strs[0][i]) {
prefix[i] = '\0'; // 不匹配,清空前缀
break;
}
}
if (j == n) {
prefix[i] = strs[0][i];
} else {
break;
}
}
printf("The longest common prefix is: %s\n", prefix);
return 0;
}
```
阅读全文