need Help with my 4 functions, any help will be appreciated.no issues at all whe
ID: 657456 • Letter: N
Question
need Help with my 4 functions, any help will be appreciated.no issues at all when I compile it but when the program executes .I then open up output file and it contains weird memory like boxes except for when I do choice 4. choice 4 works except that it doesnt print the duplicate number after the word.
#include<stdlib.h>
#include<stdio.h>
void menu()
{
printf("Choose and option below to modify your wordlist ");
printf("1.Change the first letter to a capital letter. ");
printf("2.Make each word lowercase ");
printf("3.Make each word uppercase ");
printf("4.Duplicate each word 10 times and display the word with it's duplicate # after it. ");
}
void capitalizeFirstLetter(FILE *inPtr, FILE *outPtr)
{
int c;
int newLine = 1;
while((c = fgetc(inPtr)!= EOF))
{
if(c == ' ')
{
newLine = 1;
}
else if(newLine == 1 && c!= ' ')
{
c = toupper();
}
else
{
newLine = 0;
}
fprintf(outPtr, "%c", c);
}
}
void makeLowerCase(FILE *inPtr, FILE *outPtr)
{
int c;
while((c = fgetc(inPtr)!= EOF))
{
c = tolower();
fprintf(outPtr, "%c", c);
}
}
void makeUpperCase(FILE *inPtr, FILE *outPtr )
{
int c;
while((c = fgetc(inPtr)!= EOF))
{
c = toupper();
fprintf(outPtr, "%c", c);
}
}
void duplicateTenTimes(FILE *inPtr,FILE *outPtr)
{
int i;
char line[50];
char dupNumber[2];
while(fgets(line,sizeof(line), inPtr))
{
for(i = 1; i < 11; i++)
{
fprintf(outPtr,"%s", line);
}
i = 1;
}
}
void main(int argc, char *argv[])
{
FILE *inPtr;
FILE *outPtr;
int c;
int choice;
if(argc != 3)
{
puts("you need to insert 3 arguments!");
}
else
{
inPtr = fopen(argv[1], "r");
if(!inPtr)
{
perror("File could not be opened for reading: ");
exit(1);
}
outPtr = fopen(argv[2], "w");
if(!outPtr)
{
perror("File could not be opened for appending: ");
exit(1);
}
}
menu();
scanf("%d", &choice);
switch(choice)
{
case 1: capitalizeFirstLetter(inPtr,outPtr);
break;
case 2: makeLowerCase(inPtr,outPtr);
break;
case 3: makeUpperCase(inPtr,outPtr);
break;
case 4: duplicateTenTimes(inPtr,outPtr);
break;
default:printf("Wrong entry, program is now ending...");
break;
}
fclose(inPtr);
fclose(outPtr);
}
Explanation / Answer
#include<stdlib.h>
#include<stdio.h>
void menu()
{
printf("Choose and option below to modify your wordlist ");
printf("1.Change the first letter to a capital letter. ");
printf("2.Make each word lowercase ");
printf("3.Make each word uppercase ");
printf("4.Duplicate each word 10 times and display the word with it's duplicate # after it. ");
}
void capitalizeFirstLetter(FILE *inPtr, FILE *outPtr)
{
char c;
int newLine = 0;
while((c = fgetc(inPtr))!= EOF)
{
if(c != ' ' && newLine==0)
{
c=toupper(c);
newLine=1;
}
fprintf(outPtr, "%c", c);
}
}
void makeLowerCase(FILE *inPtr, FILE *outPtr)
{
int c;
while((c = fgetc(inPtr))!= EOF)
{
c = tolower(c);
fprintf(outPtr, "%c", c);
}
}
void makeUpperCase(FILE *inPtr, FILE *outPtr )
{
int c;
while((c = fgetc(inPtr))!= EOF)
{
c = toupper(c);
fprintf(outPtr, "%c", c);
}
}
void duplicateTenTimes(FILE *inPtr,FILE *outPtr)
{
int i;
char line[50];
char dupNumber[2];
while(fgets(line,sizeof(line), inPtr))
{
for(i = 1; i < 11; i++)
{
fprintf(outPtr,"%d %s",i,line);
}
}
}
void main(int argc, char *argv[])
{
FILE *inPtr;
FILE *outPtr;
int c;
int choice;
if(argc != 3)
{
puts("you need to insert 3 arguments!");
}
else
{
inPtr = fopen(argv[1], "r");
if(!inPtr)
{
perror("File could not be opened for reading: ");
exit(1);
}
outPtr = fopen(argv[2], "w");
if(!outPtr)
{
perror("File could not be opened for appending: ");
exit(1);
}
}
menu();
scanf("%d", &choice);
switch(choice)
{
case 1: capitalizeFirstLetter(inPtr,outPtr);
break;
case 2: makeLowerCase(inPtr,outPtr);
break;
case 3: makeUpperCase(inPtr,outPtr);
break;
case 4: duplicateTenTimes(inPtr,outPtr);
break;
default:printf("Wrong entry, program is now ending...");
break;
}
fclose(inPtr);
fclose(outPtr);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.