Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  two dimensional arrays
  4. //
  5. //  Created by Mudit Jain on 7/14/18.
  6. //  Copyright © 2018 Mudit Jain. All rights reserved.
  7. //
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. // Returns 1 if word is in list. list must have length strings in it.
  12. int searchForWord(char list[][20], int length, char word[]) {
  13.     int i;
  14.     // Go through each word.
  15.     for (i = 0; i < length; i++) {
  16.         // See if it's a match.
  17.         if (strcmp(list[i], word) == 0)
  18.             return 1;
  19.     }
  20.     // If we get here, no match was found.
  21.    
  22.     return 0;
  23. }
  24. int main() {
  25.     char ** words=(char**)malloc(20*sizeof(char));
  26.     FILE* ifp;
  27.     ifp=(FILE *)malloc(sizeof(char));
  28.     char *searchword=(char*)malloc(20*sizeof(char));
  29.  
  30.     // Open the input file (required to have 10 words).
  31.     ifp = fopen("words.txt", "r");
  32.     // Read in the words into the array words.
  33.     int i;
  34.     for (i = 0; i < 10; i++)
  35.     {
  36.         *(words+i)=(char*)malloc(20*sizeof(char));
  37.         fscanf(ifp,"%s", words[i]);
  38.         printf("%s\n", words [i]);
  39.         free(*(words+i));
  40.         }
  41.  
  42.    
  43.        
  44.     // Get the word to search for.
  45.     printf("Enter a word for which to search?\n");
  46.     scanf("%s", searchword);
  47.     // Print out an appropriate message, based on the search results.
  48.     if (searchForWord(words, 10, searchword))
  49.         printf("Your word was in the list!\n");
  50.     else printf("Sorry, your word was NOT on the list.\n");
  51.     system("PAUSE");
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement