-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path08_minimum_distance.c
72 lines (59 loc) · 1.96 KB
/
08_minimum_distance.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
// // Given a list of words followed by two words, the task is to find the minimum distance between the given two words in the list of words.(Example : s = {“the”,”quick”,”brown”,”fox”,”quick”} word1 = “the”, word2 = “fox”, OUTPUT : 1)
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_ROWS 10
#define MAX_COLS 31
// // Main Function Start
int main()
{
const int ROWS = 8;
char words[8][MAX_COLS] = {
"apple",
"kiwi",
"banana",
"kiwi",
"apple",
"date",
"apple",
"banana"};
char word1[MAX_COLS], word2[MAX_COLS];
int index1 = -1, index2 = -1, minDis = ROWS - 1;
// // Display List of Words
printf("\n>>>>>>>>>>>>> List of Words <<<<<<<<<<<\n");
for (int i = 0; i < ROWS; i++)
puts(words[i]);
// // Read Two Words
printf("\n>>> Enter Two Words From the List to Find Minimum Distance Between <<<<<<<<\n");
printf("\nEnter Word-1 (MAX CHARACTERS %d) => ", MAX_COLS - 1);
fflush(stdin);
fgets(word1, MAX_COLS, stdin);
word1[strcspn(word1, "\n")] = '\0';
printf("\nEnter Word-2 (MAX CHARACTERS %d) => ", MAX_COLS - 1);
fflush(stdin);
fgets(word2, MAX_COLS, stdin);
word2[strcspn(word2, "\n")] = '\0';
// // Find Minimum Distance
for (int i = 0; i < ROWS; i++)
{
if (strcmp(word1, words[i]) == 0)
index1 = i;
if (strcmp(word2, words[i]) == 0)
index2 = i;
if (index1 != -1 && index2 != -1 && index1 <= index2)
{
if (index2 - index1 < minDis)
minDis = index2 - index1;
}
}
if (index1 != -1 && index2 != -1)
printf("\nMinimum Distance Between \"%s\" and \"%s\" => %d", word1, word2, minDis);
else
printf("\n\"%s\" Or \"%s\" not Present in the List of words", word1, word2);
putch('\n');
getch();
return 0;
}
// // Main Function End