Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // two dimensional arrays
- //
- // Created by Mudit Jain on 7/14/18.
- // Copyright © 2018 Mudit Jain. All rights reserved.
- //
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- // Returns 1 if word is in list. list must have length strings in it.
- int searchForWord(char list[][20], int length, char word[]) {
- int i;
- // Go through each word.
- for (i = 0; i < length; i++) {
- // See if it's a match.
- if (strcmp(list[i], word) == 0)
- return 1;
- }
- // If we get here, no match was found.
- return 0;
- }
- int main() {
- char ** words=(char**)malloc(20*sizeof(char));
- FILE* ifp;
- ifp=(FILE *)malloc(sizeof(char));
- char *searchword=(char*)malloc(20*sizeof(char));
- // Open the input file (required to have 10 words).
- ifp = fopen("words.txt", "r");
- // Read in the words into the array words.
- int i;
- for (i = 0; i < 10; i++)
- {
- *(words+i)=(char*)malloc(20*sizeof(char));
- fscanf(ifp,"%s", words[i]);
- printf("%s\n", words [i]);
- free(*(words+i));
- }
- // Get the word to search for.
- printf("Enter a word for which to search?\n");
- scanf("%s", searchword);
- // Print out an appropriate message, based on the search results.
- if (searchForWord(words, 10, searchword))
- printf("Your word was in the list!\n");
- else printf("Sorry, your word was NOT on the list.\n");
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement