编写一个函数来查找字符串最长公共前缀。
#include<stdio.h>
char *longestCommonPrefix(char strs[1024][1024], int strsSize)
{
for(int i = 1; i < strsSize; i++)
{
for(int j = 0; strs[0][j] != 0; j++)
{
if(strs[0][j] == 0 || strs[i][j] == 0)
{
strs[0][j] = 0;
break;
}
if(strs[0][j] != strs[i][j])
{
strs[0][j] = 0;
break;
}
}
}
return strs[0];
}
int main()
{
char strs[1024][1024] = {"abccc", "a", "a"};
char *result = longestCommonPrefix(strs, 3);
printf("%s\n", result);
return 0;
}