-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_prefix.c
96 lines (77 loc) · 1.85 KB
/
common_prefix.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Given a array of n strings, find the longest common prefix among all strings present in the array.
Input:
The first line of the input contains an integer T which denotes the number of test cases to follow. Each test case contains an integer n. Next line has space separated n strings.
Output:
Print the longest common prefix as a string in the given array. If no such prefix exists print "-1"(without quotes).
Constraints:
1<=T<=200
1<=n<=100
1<=|S|<=100
Example:
Input:
2
4
geeksforgeeks geeks geek geezer
3
apple ape april
Output:
gee
ap
*/
#include<stdio.h>
#include<stdlib.h>
char * prefix_finder(char**,int);
int main() {
int num_test=0;
int num_char=0;
int index_t,index_c;
char ***ptr;
index_t=index_c=0;
printf("Enter the number of test cases \n");
scanf("%d",&num_test);
ptr=malloc(sizeof(char)*num_test);
while(index_t<num_test) {
printf("Enter the number of strings \n");
scanf("%d",&num_char);
ptr[index_t]=malloc(sizeof(char)*num_char);
index_c=0;
while(index_c<num_char) {
ptr[index_t][index_c]=malloc(sizeof(char)*100);
scanf("%s",ptr[index_t][index_c]);
index_c++;
}
printf("%s",prefix_finder(ptr[index_t],index_c-1));
index_t++;
}
}
char* prefix_finder(char **ptr, int num_str) {
char *string1;
char *string2;
int string_index, char_index;
int str_len1, str_len2;
char substring[100];
char *tmp_count;
str_len1=str_len2=string_index=char_index=0;
tmp_count=substring;
while (string_index<num_str) {
if (string_index==0) {
string1=ptr[string_index];
string2=ptr[string_index+1];
} else {
string1=substring;
string2=ptr[string_index+1];
}
char_index=0;
while (*string1==*string2) {
substring[char_index]=*string2;
string1++;
string2++;
char_index++;
}
substring[char_index]='\0';
string_index++;
}
printf("Longest prefix is %s \n",substring);
return substring;
}