Write a program that capitalizes every other word in an input sentence until pun
ID: 3762179 • Letter: W
Question
Write a program that capitalizes every other word in an input sentence until punctuation is reached. After the punctuation, the rest of the sentence should be as is in the input sentence.
This is the code I written and it works but im having issues with the punctuation. I dont understand how to put the puntuation loop and then everything after punctuation is lower case. Please Helpppppp!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
int i=0;
int j=0;
int k=-1;
cout<< "Type a sentece: ";
getline (cin, s);
for (i=0; i<=s.length(); i++){
for (j=k+1; isspace(s.at(j))==false; j++){
cout<< s[j];
}
cout<<' ';
for (k=j+1; isspace(s.at(k))==false; k++){
s[k] = toupper(s.at(k));
cout<< s[k];
;
}
cout<<' ';
}
return 0;
}
Explanation / Answer
#include<iostream>
#include<ctype.h>
#include<string.h>
using namespace std;
int main()
{
string s;
cout << "Input a sentence : ";
getline(cin,s);
int len = (int)s.length();
int temp;
for(int i=0;i<len;i++)
{
if(ispunct(s[i]))
break;
else
{
if(i==0 || s[i-1]==' ')
{
temp = s[i];
if(temp>=97 && temp<=122)
temp = temp-32;
s[i] = temp;
}
}
}
cout << "String is : " << s;
return 0;
}
OP:
Input a sentence : wayne rooney is great! footballer
String is : Wayne Rooney Is Great! footballer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.