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

Create a C++ program that will read a series of titles from an input file (Do no

ID: 3762395 • Letter: C

Question

Create a C++ program that will read a series of titles from an input file (Do not use fstream), one per line. The titles will be in an unorthodox mix of upper and lowercase letters. Reformat each title so that the first letter of each word is capitalized and all remaining letters are lowercase. For example, "thE CAT in tHe hat" becomes "The Cat In The Hat". Assume there are no leading spaces before the first word of each title and that each word is separated by 1 blank space. At least one function (in addition to main) must be used.

Explanation / Answer

#include<iostream>

using namespace std;

char *convert(char *in) {

char *out = new char[1024];

  

int i=0;

int flag=1;

while (in[i] != '') {

if (flag == 1) {

out[i]=toupper(in[i]);

flag=0;

} else {

out[i]=tolower(in[i]);

  

if (out[i] == ' ') {

flag=1;

}

}

i++;

}

out[i]='';

return out;

}

int main() {

FILE *fp;

fp=fopen("sample.txt", "r");

char * line = NULL;

size_t len = 0;

ssize_t read;

  

if (fp != NULL) {

while ((read = getline(&line, &len, fp)) != -1) {

cout<<convert(line)<<" ";

}

} else {

cout<<"Sorry!! file cannot be opened!!";

}

  

cout<<" ";

return 0;

}