Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

LANGUAGE C 1) Implement an InputData() function that has a char string as a para

ID: 3913824 • Letter: L

Question

LANGUAGE C

1) Implement an InputData() function that has a char string as a parameter and returns an integer. The function should get a filename from the user, then attempt to open that file in read mode. If the file can’t be opened, then output an error message and return -1. Otherwise, read the string from the opened file into the function parameter using fgets(), close the file, and return a 0. Enter input text file name: myfile.txt Could not open file myfile.txt

2) Implement an OutputData() function that has a constant string as a parameter and returns an integer. The function should get a filename from the user, then attempt to open that file in write mode. If the file can’t be opened, then return -1. Otherwise, write the string to the opened file, close the file, and return a 0. Enter output text file name: myfile-out.txt Could not open file data-out.txt

3) Modify your main() program so that it no longer gets an input string from the user.

4) Modify your PrintMenu() function to include two new menu options, ‘i’ and ‘o’. MENU i – Input data from text file o – Output text to text file c - Number of non-whitespace characters w - Number of words f - Fix capitalization r - Replace all !'s s - Shorten spaces q - Quit Choose an option:

5) Modify the nested if-else statement in your main() function to include the new ‘i’ menu option. The program should call the InputData() function, and if it doesn’t return a -1, print the text that was read into the string onto the screen. Choose an option: i Enter input text file name: data.txt Text read in from file: databases of the future are certain to do more than just store numbers! many database systems already have sophisticated report engines (aka "business intelligence"), that do much of the work currently touted by the buzzword "big data!"

6) Modify the nested if-else statement in your main() function to include the new ‘o’ menu option. The program should call the OutputData() function, and if it doesn’t return a -1, print that the text was successfully written to the file. Choose an option: o Enter output text file name: data-out.txt Text written to output file.

I have to test this with this statement:

program that I have already completed that goes with this.

#include <stdio.h>

#include <ctype.h>

#include <string.h>

char PrintMenu()
{

char letterInput;

printf("Menu ");

printf("c - Number of non-whitespace characters ");

printf("w - Number of words ");

printf("f - Fix capitalization ");

printf("r - Replace all !'s ");

printf("s - Shorten spaces ");

printf("q - Quit ");

printf(" ");

printf("Choose an option:");

scanf(" %c", &letterInput, 1);

if (!(letterInput == 'c' || letterInput == 'w' || letterInput == 'f' || letterInput == 'r' || letterInput == 's' || letterInput == 'q'))
{

printf("Invalid menu option ");
return PrintMenu();

}

return letterInput;

}

int GetNumOfWSCharacters(const char string[]) {

int i;

int stringLength = 0;

for (i = 0;i < strlen(string); i++) {

if (!isspace(string[i])) {

++stringLength;

}

}

//printf("Number of non whitespace character: %d ", string);

return stringLength;

}

int GetNumOfWords(const char string[]) {

int j;

int stringWords = 0;

for (j = 0; j <= strlen(string); j++)

if (isspace(string[j])) {

++stringWords;

}

return stringWords;

}

char* FixCapitalization(char string[])
{

/*int a;

char lastChar = '_';

for (a = 0; a < strlen(string); ++a) {

if(string[a] == (>='a' && <= 'z') &&(lastChar == '.' || lastChar == '!' || lastChar == '?'))

}

}*/

int len = strlen(string);

string[0] = toupper(string[0]);

int ind = 0;

int i;

for (i = len - 1; i >= 1; i--)

{

if (!(string[i] == '.' || string[i] == '?' || string[i] == '!'));

{

if (isalpha(string[i]))

ind = i;

}

if (string[i] == '.' || string[i] == '?' || string[i] == '!')

{

string[ind] = toupper(string[ind]);

}

}

return string;

}

char* ReplaceExclamation(char string[])
{

int i;

for (i = 0; i != '';++i) {

if (string[i] == '!')

string[i] = '.';

}

return string;

}

char* ShortenSpaces(char string[])
{

char outputString[256] = " ";

int inputI = 0;

int outputI = 0;

while (string[inputI] != '') {

outputString[outputI] = string[inputI];

if (string[inputI] == ' ') {

while (string[inputI + 1] == ' ') {

inputI++;

}

}

outputI++;

inputI++;

}

outputString[outputI] = '';

return outputString;

}

int main(void)
{

char userInput[256];

char menuChoice;

int numOfNonWSChar;

int numOfWords;

char* lettersCap;

printf("Enter a sample text: ");

fgets(userInput, 256, stdin);

printf(" ");

printf(" ");

printf("You entered:");

printf("%s ", userInput);

printf(" ");

menuChoice = PrintMenu();

numOfNonWSChar = GetNumOfWSCharacters(userInput);

numOfWords = GetNumOfWords(userInput);

lettersCap = FixCapitalization(userInput);

//printf(" %c", menuChoice); Not needed so far!

do
{

//printf("%d", PrintMenu());

if (menuChoice == 'c')
{

printf("Number of non whitespace characters:");

printf("%d", numOfNonWSChar);

printf(" ");

}

else if (menuChoice == 'w')
{

printf("Number of words:");

printf("%d", numOfWords);

printf(" ");

}

else if (menuChoice == 'f')
{

printf("Fix capitalization:");

printf("%s", lettersCap);

printf(" ");

}

else if (menuChoice == 'r') {

printf("Replace all !'s: ");

printf("%s", ReplaceExclamation(userInput)); // Instead of using a new a variable I realized I could just call the function like this.

printf(" ");

//scanf_s(" %c", menuChoice);

}

else if (menuChoice == 's')
{

printf("Shorten spaces: ");

printf("%s", ShortenSpaces(userInput));

printf(" ");

//scanf_s(" %c", menuChoice);

}
menuChoice=PrintMenu();

}while (menuChoice != 'q');

return 0;

}

Explanation / Answer

#include<stdio.h>

#include<string.h> // Included for using strcpy() in InputData() function

int InputData(char str[])

{

FILE *fp; // FILE pointer

char fileName[20],temp_str[1000];

printf("Enter input text file name: ");

scanf("%s", fileName);

fp = fopen(fileName, "r"); //Opening file

if(fp == NULL)

{

printf("Could not open file %s", fileName);

return -1;

}

fgets(temp_str,1000,fp); //Read string from file into temp_str

strcpy(str,temp_str); //Copy temp_str to function parameter

fclose(fp); //Closing file

return 0;

}

int OutputData(char str[])

{

FILE *fp; // FILE pointer

char fileName[20];

printf("Enter output text file name: ");

scanf("%s", fileName);  

fp = fopen(fileName, "w"); //Opening file

if(fp == NULL)

{

printf("Could not open file %s", fileName);

return -1;

}

fprintf(fp,str); //Write string in function parameter to the file

fclose(fp);

return 0;

}

char PrintMenu()

{

char letterInput;

printf("Menu ");

printf("i - Input data from text file ");

printf("o - Output text to text file ");

printf("c - Number of non-whitespace characters ");

printf("w - Number of words ");

printf("f - Fix capitalization ");

printf("r - Replace all !'s ");

printf("s - Shorten spaces ");

printf("q - Quit ");

printf("Choose an option:");

scanf(" %c", &letterInput, 1);

if (!(letterInput == 'i' || letterInput == 'o' || letterInput == 'c' || letterInput == 'w' || letterInput == 'f' || letterInput == 'r' || letterInput == 's' || letterInput == 'q'))

{

printf("Invalid menu option ");

return PrintMenu();

}

return letterInput;

}

int main(void)

{

char menuChoice;

int numOfNonWSChar;

int numOfWords;

char* lettersCap,read_str[1000];

menuChoice = PrintMenu();

numOfNonWSChar = GetNumOfWSCharacters(userInput);

numOfWords = GetNumOfWords(userInput);

lettersCap = FixCapitalization(userInput);

//printf(" %c", menuChoice); Not needed so far!

do

{

//printf("%d", PrintMenu());

if (menuChoice == 'i')

{

if(InputData(read_str) == 0)

{

printf("Text read in from file: ");

printf("%s",read_str);

}

printf(" ");

}

else if (menuChoice == 'o')

{

if(OutputData() == 0)

{

printf("Text was successfully written to the file. ");

}

}

else if (menuChoice == 'c')

{

printf("Number of non whitespace characters:");

printf("%d", numOfNonWSChar);

printf(" ");

}

else if (menuChoice == 'w')

{

printf("Number of words:");

printf("%d", numOfWords);

printf(" ");

}

else if (menuChoice == 'f')

{

printf("Fix capitalization:");

printf("%s", lettersCap);

printf(" ");

}

else if (menuChoice == 'r')

{

printf("Replace all !'s: ");

printf("%s", ReplaceExclamation(userInput)); // Instead of using a new a variable I realized I could just call the function like this.

printf(" ");

//scanf_s(" %c", menuChoice);

}

else if (menuChoice == 's')

{

printf("Shorten spaces: ");

printf("%s", ShortenSpaces(userInput));

printf(" ");

//scanf_s(" %c", menuChoice);

}

menuChoice=PrintMenu();

}while (menuChoice != 'q');

return 0;

}

/* All 6 questions have been answered. Incase you have any query regarding the answer kindly write it down in the comments. Thanks*/