In: Computer Science
using c++. ALWAYS GRADE MY ANSWERS
nstructions
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
For example, it should output the individual digits of:
#2
Instructions
Write a program that
prompts the user to input a sequence of characters and outputs the
number of vowels.
(Use the function isVowel written in Programming Exercise
2.)
Your output should look like the following:
There are # vowels in this sentence.
... where # is the number of vowels.
#3
Instructions
You are given a file consisting of students’ names in the following form: lastName, firstName middleName. (Note that a student may not have a middle name.)
Write a program that converts each name to the following form: firstName middleName lastName. Your program must read each student’s entire name in a variable and must consist of a function that takes as input a string, consists of a student’s name, and returns the string consisting of the altered name. Use the string function find to find the index of ,; the function length to find the length of the string; and the function substr to extract the firstName, middleName, and lastName
here is the file:
Miller, Jason
Brian
Blair, Lisa Maria
Gupta, Anil Kumar
Arora, Sumit Sahil
Saleh, Rhonda Beth
Spilner, Brody
Screenshot of question1:
The Output Obtained:
The Code Used:
#include<iostream>
using namespace std;
int main()
{
int k = 0;
int number;
int arr[20]; //this array will store the individual
digits of the integer.
int sum = 0;
cout<<"Enter an integer: "; //asking the user to
enter a number.
cin>>number;
if(number<0) //if the number is negative we turn it
positive by multipling -1.
{
number*=-1;
}
while(number>0) //we extract each digit from the
last and store it in the array.
{
arr[k++] = number%10;
number/=10;
}
for(int i=k-1;i>=0;i--) //this array is printing
from k-1 to 0 cause the array contains the digits in reverse
order.
{
cout<<arr[i]<<" ";
//printing the digits.
sum+=arr[i]; //calculating the sum
of the sums.
}
cout<<endl;
cout<<"The sum of the digits are:
"<<sum<<endl; //printing the sum.
return 0;
}
The Screenshot Of Question2:
The Output Obtained:
The Code Used:
#include<iostream>
using namespace std;
int isVowel(char ch) //checking if the character is vowel or not
and returning 1 or 0 respectively.
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
return 1;
}
return 0;
}
int main()
{
int vowelCount = 0;
string str;
cout<<"Enter a string: "; //taking the string a
input from the user.
getline(cin,str);
for(int i=0;i<str.size();i++) //iterating through
the string and counting the numer of vowels.
{
if(isVowel(str[i]))
{
vowelCount++;
}
}
cout<<"There are "<<vowelCount<<"
vowels in this sentence."<<endl; //printing the
vowelCount.
return 0;
}
The Screenshot of Question3:
The Ouput Obtained:
The Code Used:
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
string studentName(string str)
{
int pos = str.find(","); //we find the position of ,
and store it in pos variable.
string lastName = str.substr(0,pos); //next we take
the slice of the string before , and store it in lastName
string.
//The line below will take the string slice after the
", " till the end therefore it will
//contain the first name and the middle name.
string firstMiddleStr =
str.substr(pos+2,str.size()-(pos+2));
return firstMiddleStr+" "+lastName; //we return the
string.
}
int main()
{
string filename;
cout<<"Enter the file name: "; //asking the
filename from the user.
cin>>filename;
ifstream in(filename); //opening the file.
if(!in) { //if and error occurs while opening the
file.
cout << "Cannot open input file.\n";
return 1;
}
string line;//this will store the lines in the text
file.
while(getline(in,line)) { //Here we read one line at a
time and call the student Name function and print the string.
cout<<studentName(line)<<endl;
}
in.close();
return 0;
}
I hope you like the solution. In case of any doubts regarding the solution feel free to ask it in the comment section. If you like the solution please give a thumbs up.