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
#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;
}
Comments
Leave a comment