Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Declare an array of pointers of char type where each pointer points to a string.

ID: 669867 • Letter: D

Question

Declare an array of pointers of char type where each pointer
points to a string. As an example, the array may be declared and initialized as
follows,
char *dictionary[10] ;
dictionary[0]= “ auto” ; dictionary[1]=”car”; dictionary[2]=”door”;
The system stores each string in a null terminated character array and then it
places the beginning address of each character array in the dictionary array. Thus
each pointer in the dictionary array points to a character array which stores one of
the strings.
Assume that these strings correspond to the words in a dictionary. The words are
in alphabetical order. Write a program which will prompt a user to enter a word
and search for it in the dictionary. If the word is not in the dictionary the program
inserts that word to the appropriate location in the dictionary. For example, after
inserting the string “bus” to the dictionary, we will have,
dictionary[0]= “ auto” ; dictionary[1]=”bus”;
dictionary[2]=”car”; dictionary[3]=”door”;
Note: The library c-string functions cannot be used in the solution of this
problem.

Explanation / Answer

#include<stdio.h>

int main(){
   char *dictionary[10] ;
   char *s;
   s = malloc(256);
   int i;  
   while(1){
       printf("Enter a string to search: ");
       scanf("%s",s);
      
       for(i = 0; i < 10 ; i++){
           if(dictionary[i] == s)
               break;
       }
       for(i = 0; i < 10 ; i++){
           if(dictionary[i] == ""){          
               dictionary[i] = s;
               break;
           }
       }
      
      
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote