write a program that reads in a sentence of up to 100 characters and outputs the
ID: 3772910 • Letter: W
Question
write a program that reads in a sentence of up to 100 characters and outputs the sentence with spacing corrected and with letters corrected for capitalization... The sentence should start with an upper case letter but should contain no other uppercase letters.
For example, the Answer to life, the Universe, and everything IS 42.
output -> The answer to life, the universe, and everything is 42.
Here's what I have so far.
#include "stdafx.h" // for Microsoft C++
#include // for cin and cout
#include //
#include // for isspace, toupper, and tolower
using namespace std;
int main(int argc, char* argv[])
{
char sentence[200];
int i;
// read sentence into the array
cout << "Enter a sentence:" << endl;
cin.getline(sentence, 200); //reads upto 200 characters
// change all whitespace characters to a space
for (i = 0; sentence[i]; i++)
{
if (isspace(sentence[i]))
sentence[i] = ' ';
}
// remove multiple spaces
i = 0;
while (sentence[i]!='')
{
if (sentence[i]== ' ' && sentence[i+1]== ' ')
for (int j = 1; sentence[j]; j++)
{
sentence[j] = sentence[j+1];
}
else
i++;
}
sentence[0] = toupper(sentence[0]); // capitalize first letter of sentence
// to make all other characters lower case
for (i = 1; sentence[1]; i++)
{
sentence[i] = tolower(sentence[i]);
}
//display output
cout << sentence << endl;
return 0;
}
Explanation / Answer
Answer :
#include "stdafx.h"
#include <iostream>
#include <cctype>
#include "windows.h"
using namespace std;
void capital(char givendata[]);
int main()
{
char givendata[100];
cout << "Enter text up to 99 characters" <<endl;
cin.getline(givendata, 100,' ');
capital(givendata);
cout <<endl;
system("PAUSE");
return 0;
}
void capital(char givendata[])
{
if(givendata[0] != ' ')
givendata[0] = toupper(givendata[0]);
for (int count = 0; count <= 99; count++)
{
if(givendata[count] == ' ' || givendata[count]== ',' || givendata[count]==';' ||givendata[count]== '.')
givendata[count+1]=toupper(givendata[count+1]);
}
cout << givendata;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.