I am stuck on the following problem below, this is in C NOT C++, thank you in ad
ID: 3540334 • Letter: I
Question
I am stuck on the following problem below, this is in C NOT C++, thank you in advance!
(Strings, Sorting) Prepare an input data file that contains 30 names. At least two of the names are identical. There is one name in each line in the following format:
Firstname Lastname
Write a program to read the names from the data file into an array. Each entry of the array should be a string in the following format:
Lastname, Firstname
Each name in the array must be unique.
Your program will display the names in the same way they are stored in the data file and in the last name-first name format in ascending alphabetical order. (Make sure this in C Programming not C++)
Explanation / Answer
please rate -thanks
any questions ask-will get back to you, probably in the morning
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{int i=0,j,k;
FILE *input;
char name[50][40],temp[50];
char first[20],last[20];
input = fopen("input.txt","r");
if(input == NULL)
{ printf("Error opening input file! ");
return 0;
}
printf("names as input ");
while(fscanf(input,"%s%s",&first,&last)>0)
{printf("%s %s ",first,last);
name[i][0]='';
strcat(name[i],last);
strcat(name[i],", ");
strcat(name[i],first);
i++;
}
printf(" names sorted ");
for(j=0;j<i-1;j++)
for(k=j+1;k<i;k++)
{if(strcmp(name[j],name[k])>0)
{strcpy(temp,name[j]);
strcpy(name[j],name[k]);
strcpy(name[k],temp);
}
}
for(j=0;j<i;j++)
{printf("%s ",name[j]);
}
fclose(input);
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.