Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ 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

C++ 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:

USE LINUX REDIRECTION. DO NOT USE FSTREAM.

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 Here is a start to the program. #include #include using namespace std; int main() { string data;

cin >> data; while (cin) { } cin >> data; } ONE FUNCTION MUST BE USED. NO OTHER HEADER FILES ARE NEEDED. DO NOT USE FSTREAM. INPUT WILL BE TAKEN THROUGH LINUX REDIRECTION Here is my code. The problem is, the onE fish two FISh red fISh bLuE Fish keeps repeating when I put in the file through cin. Please help me fix my code:

#include #include

using namespace std; //Forward declaration void FormatString(string); void PrintString(string);

int main() { string data; // take string input do { std::getline(std::cin,data); FormatString(data); // Call this function for formatting the string }while(!data.empty());// if no input it will terminate return 0; }

void FormatString(string data) { for(int i =0; data[i] !='\0';) { if((data[i] - 'a' >= 0 && data[i] - 'a' < 26) || (data[i] - 'A' >= 0 && data[i] - 'A' < 26)) { data[i] = toupper(data[i]); // Convert first letter to upper i++; } while(data[i] != ' ' && data[i] != '\0') { data[i] = tolower(data[i]);// convert all letters in a word other than first to lower case i++; }

if(data[i] == '\0') // if its end break break; else while(data[i] == ' ') i++; // skip multiple spaces } PrintString(data); // function for printing string }

void PrintString(string data) { int spaceCount =0; for(int i =0;i < data.length(); i++) { if(data[i] != ' ') { cout << data[i] ; // keep printing characters until you encounter space spaceCount = 0; } else { if(spaceCount < 1) cout << data[i]; // Print only one space and consume other spaces spaceCount++; } } cout<<" "; }

// output

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions