Answer to Question #325221 in C for Muskan

Question #325221

Suppose NAME is a 8 element array and 5 elements (Brown,Davis,Johnson,Smith,wagner)are in array observe that names are listed alphabetically and keep it alphabetical all times suppose ford and Taylor are added to the array and Davis removed from array

1
Expert's answer
2022-04-07T07:30:25-0400
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #define n 8
 #define m 10
 
 int main()
 {
    char NAME[n][m] = {"Brown" ,"Davis", "Johnson", "Smith" ,"Wagner", "", "", ""};
    int i, j, cnt;
    cnt = 5;
    printf("-----source array-------\n");
    for (i=0; i<cnt; i++)
    {
       printf("%s\n", NAME[i]);
    }
    cnt++;
    strcpy(NAME[cnt-1], "Ford");
    cnt++;
    strcpy(NAME[cnt-1], "Taylor");
    printf("-----after Ford and Taylor are added------\n");
    for (i=0; i<cnt; i++)
    {
       printf("%s\n", NAME[i]);
    }
    qsort(NAME, cnt, sizeof(char[m]), (int (*)(const void *,const  void *)) strcmp);
    
    printf("-----sort array------\n");
    for (i=0; i<cnt; i++)
    {
       printf("%s\n", NAME[i]);
    }
    
    for (i=0; i<cnt; i++)
    {
       if (strcmp(NAME[i], "Davis") == 0)
       {
               for (j=(i+1); j<cnt; j++)
                   strcpy(NAME[j-1], NAME[j]);
               strcpy(NAME[j-1], "");
            break;
       }
    }
    
    printf("-----after Davis is removed------\n");
    for (i=0; i<cnt; i++)
    {
       printf("%s\n", NAME[i]);
    }
     
    return 0;
 }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS