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

New Skills Practiced (Learning Goals) Problem solving and debugging. User-define

ID: 3809241 • Letter: N

Question

New Skills Practiced (Learning Goals)

Problem solving and debugging.

User-defined functions.

C++ strings.

Design a program that will read a series of titles from an input file (via Linux redirection), one per line.

The titles will be in an unorthodox mix of upper and lowercase letters and words may be separated by more than 1 blank.

Reformat each title so that

the first character (if it is a letter) of each word is capitalized and all remaining letters are lowercase

each word is separated by exactly one blank space

For example, "thE     CAT in tHe hat" becomes "The Cat In The Hat".

REQUIREMENTS

At least one function (in addition to main) must be used.

The string data type must be used.

Never use global variables.

Never use goto statements.

File can only be read one time.

The required output (displayed to the screen) from the program is

a list of the reformatted titles, one per line

ASSUMPTIONS

The input file will not be empty.

There will be one title per line.

Each title will consist of 1 or more words.

There will be no leading spaces before the first word in the title.

There will be at least 1 blank space between each word (there may be more).

There will be a linefeed after the last character of the last word in a title (no trailing blanks).

The program need only attempt to capitalize the first character of a word. For example, if a word in the title is "9TH", it should be reformatted to "9th". In other words, the program does not have to find the first letter in a word and capitalize it.

NOTES:

Make sure you choose enough test data to ensure that your program meets all the requirements.

Sample terminal session:

[keys]$ more data4ten
the       5TH    wAVE
the cAT in thE   HAT
onE fish two FISh     red fISh bLuE Fish
[keys]$ g++ exercise10.cpp
[keys$ ./a.out < data4ten

The 5th Wave
The Cat In The Hat
One Fish Two Fish Red Fish Blue Fish

Explanation / Answer

/*
* C Program to Capitalize First Letter of every Word in a File
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
int to_initcap_file(FILE *);

void main(int argc, char * argv[])
{
FILE *fp1;
char fp[10];
int p;

fp1 = fopen(argv[1], "r+");
if (fp1 == NULL)
{
printf("cannot open the file ");
exit(0);
}
p = to_initcap_file(fp1);
if (p == 1)
{
printf("success");
}
else
{
printf("failure");
}
fclose(fp1);
}

/* capitalizes first letter of every word */
int to_initcap_file(FILE *fp)
{
char c;

c = fgetc(fp);
if (c >= 'a' && c <= 'z')
{
fseek(fp, -1L, 1);
fputc(c - 32, fp);
}
while(c != EOF)
{
if (c == ' ' || c == ' ')
{
c = fgetc(fp);
if (c >= 'a' && c <= 'z')
{
fseek(fp, -1L, 1);
fputc(c - 32, fp);
}
}
else
{
c = fgetc(fp);
}
}
return 1;
}