c - Searching type char** for a string -


note repost, have clarified post more understandable

void searcharray(char ***array, int count){      char * word = "";       printf("enter word: ");     fscanf(stdin, " ");     fscanf(stdin, "%c", &word);     bool found = false;     for(int = 0; < count; i++){             if(strcmp(word, (*array)[i]) == 0){                     found = true;                     break;             }     }     if(found) printf("%s found!\n", word);     else if (!found) printf("%s not found!\n", word); } 

in testing, code returns " not found!" every input.

the above code have searching , traversing array of type char ** ... i'm not sure whether have traversing logic wrong or if i'm improperly using strcmp... appreciated!

here code insertion may clarify i'm trying do:

int insertword(char **array, int *count, char word[]) {    char *wordptr;     wordptr = (char *)malloc((strlen(word) + 1) * sizeof(char));    if (wordptr == null)    {       fprintf(stderr,"    malloc of array[%d] failed!\n", *count);       return -1;    }    /* memory word has been allocated, copy characters       , insert array */     strcpy(wordptr, word);     array[*count] = wordptr;    (*count)++;     return 0; } 

my task search specific string in data.

void searcharray(char ***array, int count){      char word[80];      printf("enter word: ");     fscanf(stdin, " ");     fscanf(stdin, "%s", word);     bool found = false;     for(int = 0; < count; i++){             if(strcmp(word, (*array)[i]) == 0){                     found = true;                     break;             }     }     if(found) printf("%s found!\n", word);     else if (!found) printf("%s not found!\n", word); } 

this code works perfectly. think since using fscanf(stdin, "%c", &word); reading in open space character previous line (in buffer) , searching it...is how works?

thanks!


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -