Untitled
unknown
plain_text
3 years ago
1.4 kB
11
Indexable
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int comp1(const void *a, const void *b)
{
const char **str_a = (const char **)a;
const char **str_b = (const char **)b;
return strcmp(*str_a, *str_b);
}
int main() {
char text[1033];
char sep[10] = " .";
char* istr;
int n_cnt = 0;
int word_cnt = 1;
int k = 0;
for (int i = 0; i<1033; i++){
k = i;
char c = getchar();
if (c == ' ') word_cnt++;
if (c == '\n'){
n_cnt++;
if (n_cnt == 1){
text[i] = ' ';
continue;
}
else break;
}
text[i] = c;
}
text[k] = '\0';
char** words = malloc(sizeof(char*) * word_cnt);
int ind = 0;
char* str = calloc(31, sizeof(char));
istr = strtok(text, sep);
while (istr != NULL){
char* word = istr;
if (ind == word_cnt) {
strncpy(str, word, strlen(word));
}
words[ind++] = word;
istr = strtok(NULL, sep);
}
qsort(words, word_cnt, sizeof(char*), comp1);
char** result = bsearch(&str, words, word_cnt, sizeof(char*), comp1);
free(str);
if (result != NULL){
printf("exists");
}
else {
printf("doesn't exist");
}
return 0;
}
Editor is loading...