Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

6. Building off your answer to the previous modify your program so that it counts how many words start with the letter "s" (both upper and lower case YOUR ANSWER HERE 7. Again, modify your answer so that it counts how many words are hyphenated in some way (have a inside them somewhere) YOUR ANSWER HERE 8. Using a string stream write a function which takes in a string" and splits it into words separated by spaces and returns a vector

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)<<" ";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote