Starting out with >>>C++: From control structures through objects, eighth editio
ID: 3769271 • Letter: S
Question
Starting out with >>>C++: From control structures through objects, eighth edition. Chapter 10, Programming Challenge 5: Sentence Capitalizer. Write a function that accepts a pointer to a C-string as an argument and capitalizes the first character of each sentence in the string. Ex. "hello. my name is Joe. what is your name?" The function should manipulate the string so it contains "Hello. My name is Joe. What is your name?" Demonstrate the function in a program that asks the user to input a string and then passes it to the function. The modified string should be displayed on the screen.
Explanation / Answer
#include <iostream>
#include <stdio.h>
#include<conio.h>
using namespace std;
//function Prototype
char *Capitalizer(char *);
int main()
{
char *newline,line[1001]; //That's A Big Buffer, It Might Be TOO Big... Careful There...
cout << "This program will capitalize the first letter of each sentence. ";
cout << "Please enter a phrase of no more than 1000 characters, followed by a period. ";
cin.getline(line, 1001);
newline = Capitalizer(line);
cout << " This is how you should have done it: ";
cout << newline;
cout << endl;
getch();
return 0;
}
char *Capitalizer(char *sentencePrt)
{
int i = 0, j;
if(sentencePrt[i] > 97 && sentencePrt[i] < 112)
{
sentencePrt[i] -= 32;
}
for(i = 0; i < strlen(sentencePrt); i++)
{
j = i;
if(sentencePrt[i] == '.' || sentencePrt[i] == '?' || sentencePrt[i] == '!' || sentencePrt[i] == ',')
{
j++;
if(sentencePrt[j] == ' ' || sentencePrt == NULL || sentencePrt == '')
{
j++;
}
if((sentencePrt[j] > 97 && sentencePrt[j] < 112))
{
sentencePrt[j] -= 32;
}
}
}
//cout << sentencePrt;
return sentencePrt;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.