Last active
September 10, 2015 00:30
-
-
Save jmkim/d71f1d272e4fae677142 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#define MAX 10000 | |
#define BUFFER_SIZE 100 | |
char *words[MAX]; | |
int n = 0; | |
void bubblesort(); | |
int find(char *); | |
int is_prefix(char *, char *); | |
int main() | |
{ | |
char buffer[BUFFER_SIZE]; | |
FILE *fp = fopen("harry.txt", "r"); | |
while (fscanf(fp, "%s", buffer) != EOF) | |
{ | |
int index = find(buffer); | |
if (index == -1) | |
words[n++] = strdup(buffer); | |
} | |
fclose(fp); | |
/* Exercise 01 */ | |
bubblesort(); | |
for (int i = (n-10 >= 0 ? n-10 : 0) ; i<n ; i++){ | |
printf("%s\n", words[i]); | |
} | |
/* Exercise 02 */ | |
printf("Enter a word: "); | |
scanf("%s", buffer); | |
if (find(buffer) != -1) | |
printf("Yes\n"); | |
else | |
printf("No\n"); | |
/* Exercise 03 */ | |
printf("Enter a prefix: "); | |
scanf("%s", buffer); | |
for (int i=0; i<n; i++) | |
if (is_prefix(buffer, words[i]) == 1) | |
printf("%s\n", words[i]); | |
return 0; | |
} | |
int find(char * word) | |
{ | |
for (int i=0 ; i<n ; i++) | |
if (strcmp(word, words[i]) == 0) | |
return i; | |
return -1; | |
} | |
int is_prefix(char *prefix, char *word) | |
{ | |
if (strlen(prefix) > strlen(word)) | |
return 0; | |
for (int i=0; i<strlen(prefix); i++) | |
if (prefix[i] != word[i]) | |
return 0; | |
return 1; | |
} | |
void bubblesort() | |
{ | |
for (int i = n - 1; i > 0; i--) | |
{ | |
for (int j = 0; j<i; j++) | |
{ | |
if (strcmp(words[j], words[j + 1]) > 0) | |
{ | |
char *tmp = words[j]; | |
words[j] = words[j + 1]; | |
words[j + 1] = tmp; | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment