Use C programming: Create a function that continuously reads in files of sentenc
ID: 3721389 • Letter: U
Question
Use C programming: Create a function that continuously reads in files of sentences in Morse code (each letter is separated by one space and each word is separated by three spaces) and translates to English. The translation should be output to a file (make sure to capitalize the first letter of each sentence). Each sentence ends with a # followed by a space-no other punctuation will be present. Part of a sample file might look like: - .... . .-- . .- - .... . .-. .. ... --. --- --- -..# .. .... --- .--. . - .... . .-- . .- - .... . .-. .. ... --. --- --- -.. - --- -- --- .-. .-. --- .--# Notice the sentences are in paragraph form and NOT line by line. Do not worry about dealing with numbers-only letters. A possible function run might look like:
---Reading in ---Translating…
What file to save to?
morse1.txt
Saved to file!
Would you like to read in another file?
y or n
y
---Reading in ---
Translating…
What file to save to?
morse2.txt
Saved to file!
Would you like to read in another file?
y or n
n
Explanation / Answer
#include<stdio.h>
void convert(char *file, char *file1)
{
FILE *fp, *fq;
fq = fopen(file,"w");
fp = fopen(file1,"r");
char c[200];
int n = 0;
while((c[0] = fgetc(fp))!=EOF)
n++;
fclose(fp);
fp = fopen(file1,"r");
fgets(c, n, fp);
for(int i = 0 ; i < n ; i++)
{
if(c[i] != ' ')
{
if(c[i-1] == ' ' && c[i-3] == ' ' && c[i-2] == ' ' && i > 3)
{
c[i] = c[i] - 32;
}
fputc(c[i], fq);
}
else if(c[i] == ' ' && c[i+1] == ' ' && c[i+2] == ' ')
{
fputc('.', fq);
}
}
}
main()
{
char file[100], file1[100];
char ch = 'y';
while(ch == 'y')
{
printf("Which file to read?: ");
scanf("%s", file1);
printf("What file to save to: ");
scanf("%s", file);
convert(file);
printf("Translated ");
printf("Would you like to read in another file? y or n");
scanf("%c", &ch);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.