// Problem #1: [] // -----------------------------------------------------------
ID: 3550960 • Letter: #
Question
// Problem #1: []
// ------------------------------------------------------------------
// Function: Complete the following program
// a) Read (using >>) a character Letter;
// b) Read (using getline) a string Line.
// c) Count the #times Letter appears in Line.
// d) Print the length of Line, the letter, and the number
// times it occurs.
//
// Example: Input: oGood Job ==> 8 o3
// AFatCat ==> 6 A0
// 8123848 8 2 => 10 83
// ------------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
string Line; char Letter;
int count=0;
// --------------------------------------------------------------------
// WARNING: DO NOT ISSUE PROMPTS or LABEL OUTPUT.
// --------------------------------------------------------------------
cout << Line.length() << " " << Letter << count;
cout << endl; // *** DO NOT REMOVE ***
return 0;
}
//Problem #2:
// ------------------------------------------------------------------
// Function: Complete the following program
// a) Read a count.
// b) Read (>>) string Word
// c) Create C-string Answer
// d) Place up to count copies of Word into C-string Answer.
// e) Print th length of Answer, followed by its value.
//
// Example: Input: 4 12345 ==> 20 12345123451234512345
// 2 FatCat ==> 12 FatCatFatCat
// 5 abc134. ==> 35 abc134.abc134.abc134.abc134.abc134.
// ------------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
string Word, Answer;
int count, times;
// --------------------------------------------------------------------
// WARNING: DO NOT ISSUE PROMPTS or LABEL OUTPUT.
// --------------------------------------------------------------------
cin >> count >> Word;
Answer = "";
cout << endl; // *** DO NOT REMOVE ***
return 0;
}
//Problem #3:
// ------------------------------------------------------------------
// Function: Complete the following program
// a) Read (>>) C-string Cstr (can hold 20 characters)
// b) Print EVEN if length of Cstr is even; ODD otherwise.
//
// Example: Input: FAT ==> ODD
// Input: CATWOMAN ==> EVEN
//
// ------------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
char Cstr[21];
// --------------------------------------------------------------------
// WARNING: DO NOT ISSUE PROMPTS or LABEL OUTPUT.
// --------------------------------------------------------------------
cout << endl; // *** DO NOT REMOVE ***
return 0;
}
Explanation / Answer
Problem 1
#include <iostream>
using namespace std;
int main()
{
string Line; char Letter;
int count=0;
int i;
cin>>Letter;
getline(cin,Line);
for(i=0;i<Line.length();i++)
{
if(Line[i]==Letter) count++;
}
cout << Line.length() << " " << Letter << count;
cout << endl; // *** DO NOT REMOVE ***
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.