-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc27.c
More file actions
39 lines (35 loc) · 954 Bytes
/
c27.c
File metadata and controls
39 lines (35 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Longest common prefix
#include<stdio.h>
#include<string.h>
#include <stdio.h>
#include <string.h>
#define MAX_WORDS 10
#define MAX_LENGTH 100
int main()
{
// Input words
char words[MAX_WORDS][MAX_LENGTH] = {
"flower",
"flow",
"flowing"
};
int size = 3;
char prefix[MAX_LENGTH];
int i, j;
// Start with the first word as prefix
strcpy(prefix, words[0]);
for (i = 1; i < size; i++)
{
// Compare prefix with current word
for (j = 0; j < strlen(prefix); j++)
{
if (prefix[j] != words[i][j])
{
break; // mismatch found
}
}
prefix[j] = '\0'; // cut the prefix at mismatch
}
printf("Longest Common Prefix: %s\n", prefix);
return 0;
}