I need help writing this code in C++ This is the prompt/ question for the progra
ID: 3696216 • Letter: I
Question
I need help writing this code in C++
This is the prompt/ question for the program i'm doing:::::::::
Sentence Capitalizer with the following exceptions: instead of asking the user to input a string and passing it to the function, initialize the C-string with the following string:
"no, not tonight. it's a very popular place and you have to make reservations in advance. besides, it's expensive, and I don't have any money."
The modified string should look like:
"No, not tonight. It's a very popular place and you have to make reservations in advance. Besides, it's expensive, and I don't have any money."
The punctuation marks that signal the end of a sentence are the period, the question mark and the exclamation mark.
So your code should work with sentences that end with the question mark and the exclamation mark too.
Yes, your program needs to include the main function that prepares the C-string and calls the function as written there in the book.
Display the text before and after the call. The output of your program should look something like the following:
The text before the modification:
no, not tonight. it's a very popular place and you have to make reservations in advance. besides, it's expensive, and I don't have any money.
The text after the modification:
No, not tonight. It's a very popular place and you have to make reservations in advance. Besides, it's expensive, and I don't have any mone
This is the code I have prepared::
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;
void sentenceCapitalizer (char *text);
int main() {
char userString [] = "no, not tonight. it's a very popular place
and you have to make reservations in advance. besides,
it's expensive, and I don't have any money.";
cout<<"The text before the modification:";
cout<<userString;
//pass the string to the function to capitalize the first character of
//every sentence
cout <<"The text after the modification:";
cout << userString << endl;
return 0;
}
//*********************************************************************************
//* sentenceCapitalizer
//*
//* This function scans through the input C-string that is passed in through the character pointer,
//* text. It capitalizes the first character that begins every sentence in the string.
//*
//* Parameters:
//* text- a character pointer that points to the first character of the string
//*
//* Returns:
//* void
//
//*********************************************************************************
void sentenceCapitalizer (char *text) {
char *ptr = text;
}
/*
Copy output of this program below this line.
--------------------------------------------
*/
Explanation / Answer
Please follow the code and comments for description :
CODE :
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
char *sentenceCapitalizer(char *sentence);
int main()
{
const int size = 500;
//char usersentence[size]; //user inputed text/sentences
char usersentence [size] = "no, not tonight. it's a very popular place
and you have to make reservations in advance. besides,
it's expensive, and I don't have any money.";
// cout << "Input a string to modify: ";
// cin.getline(usersentence, size);
cout << "String before modification : " << usersentence;
cout << " Your modified string is below. ";
cout << sentenceCapitalizer(usersentence);
cout << endl << endl;
system("pause");
}
//fnc to capitalize each sentence
char *sentenceCapitalizer(char *sentence)
{
int length = strlen(sentence); //used to get the length of user string
int i, j; //integers for the for-loop. Will use both to decide char to capitalize
sentence[0] = toupper(sentence[0]); //capitalize the first letter
for (i = 0; i < length; i++) //used to search the string
{
j = i;
if (sentence[i] == '.' || sentence[i] == '?' || sentence[i] == '!') //to search for punctuation
{
j++;
j++;
sentence[j] = toupper(sentence[j]);
if (sentence[j] == ' ') //for doulble spaced sentences
{
j++;
sentence[j] = toupper(sentence[j]);
}
}
}
return sentence;
}
OUTPUT :
String before modification :
no, not tonight. it's a very popular place and you have to make reservations in advance. besides, it's expensive, and I don't have any money.
Your modified string is below.
No, not tonight. It's a very popular place and you have to make reservations in advance. Besides, it's expensive, and I don't have any money.
NOTE :
The above code can be made an interactive code by uncommenting the lines of code so as to get the user desired string as the input, else can be left commented.
Hope this is useful.