Read a set of strings from the keyboard. Store the input strings to a file “stri
ID: 3768902 • Letter: R
Question
Read a set of strings from the keyboard. Store the input strings to a file “string.txt” in alphabet order. Please include the problem-solving process. You also need to handle all possible exceptions. it more like a text file. you have to read a strings from the keyboard. that all my instructor posted .he did not provide any other information apart what i sent you. for example you can enter janet, luis and put them in alphabetical order in a file. i hope this information helps you write the program thanks
Explanation / Answer
#include <stdio.h>
#include<string.h>
int main() {
char s[5][20], t[20];
int i, j;
FILE *fptr;
fptr=fopen("D:program.txt","w");
if(fptr==NULL){
printf("Error!");
exit(1);
}
printf(" Enter any five strings : ");
for (i = 0; i < 5; i++)
scanf("%s", s[i]);
for (i = 1; i < 5; i++) {
for (j = 1; j < 5; j++) {
if (strcmp(s[j - 1], s[j]) > 0) {
strcpy(t, s[j - 1]);
strcpy(s[j - 1], s[j]);
strcpy(s[j], t);
}
}
}
for (i = 0; i < 5; i++)
fprintf(fptr," %s", s[i]);
fclose(fptr);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.