C++ Introduction to Programming It is short and simple practice. Please solve #6
ID: 3774842 • Letter: C
Question
C++ Introduction to Programming
It is short and simple practice.
Please solve #6, 7, 8, 9, 10
#include<iostream>
...
Classes: members and methods
Classes, continued: access specifiers and constructors
Classes for object oriented design
Recursion
Multi-file projects and header files
Exceptions, tests, and assertions
Assertions, Advanced Input/Output
Operator Overloading; Inheritance and Abstract data types
Explanation / Answer
6. Ans:
#include <iostream>
#include<string>
using namespace std;
int main()
{
string str;
int words = 0;
cout << "Enter your string " << endl;
getline(cin, str);
//cout<<str<<" ";
if(str[0] == 's' || str[0] == 'S' )
words++;
for(int i = 0; i<str.length(); i++)
if(str[i] == ' ' || str[i+1] == ' ' )
if(str[i+1] == 'S' || str[i+1] == 's' )
words++;
cout<<"Number of words start with letter 's' : "<<words<<" ";
return 0;
}
7. Ans :
#include <iostream>
#include<string>
using namespace std;
int main()
{
string str;
int words = 0;
cout << "Enter your string " << endl;
getline(cin, str);
cout<<str<<" ";
for(int i = 0; i<str.length(); i++)
if(str[i] == '-')
words++;
cout<<"Number of hyphenated words : "<<words<<" ";
return 0;
}
8. Ans :
#include <iostream>
#include<string>
#include<vector>
#include<sstream>
#include<iterator>
using namespace std;
vector<string> split(string str){
string buf;
stringstream ss(str);
istream_iterator<string> begin(ss);
istream_iterator<string> end;
vector<string> tokens(begin, end);
return tokens;
}
int main()
{
string str;
vector<string> word;
int words = 0;
cout << "Enter your string " << endl;
getline(cin, str);
word = split(str);
cout<<" string splited by space ";
copy(word.begin(), word.end(),ostream_iterator<string>(cout," "));
}
9. Ans :
#include <iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
int main()
{
string str;
while(1){
if (cin.eof()) //handle the press ctrl+D
return 0;
cout << "Enter your string " << endl;
getline(cin, str); //getline() scan the string with spaces
ofstream outfile("input.txt",ios::out|ios::app);
outfile << str;
outfile << " ";
}
}
10. Ans :
#include <iostream>
#include<string>
#include<vector>
#include<fstream>
#include<sstream>
using namespace std;
vector<int> vector_from_file(const char *file) {
string word;
vector<int> num;
int n;
ifstream out(file);
while(out >> word){
// cout<<word<<" ";
stringstream convert(word); convert >> n;
num.push_back(n);
}
return num;
}
int main()
{
char file[10];
vector<int> num;
cout<<"Enter input file name : ";
cin>>file;
num = vector_from_file(file);
for(int i=0; i<num.size(); i++)
cout<<num.at(i)<<" ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.