s. Define each of the following functions Use x as a double variable. The return
ID: 3890882 • Letter: S
Question
s. Define each of the following functions Use x as a double variable. The return type is also a floating point oumber Ans: b) Define a function that takes two strings first and last and returns the value by joining the two strings by putting a space in the middle: First declare a third string call it full, and then full-string1+ “ “ + string2 Ans: e) Define a function that takes one string (x) as input, and it should display the string by reversing the characters. This function does not return any value. Hints: First you find out how many characters are there in the string and store that in an integer variable n as: n x.sizeO Then write a loop to display one character at a time starting from last character The first character of the string is referred as: x[0), the last character of the string is referred as x[n-1] Ans: ShiftExplanation / Answer
#include<iostream>
#include<string>
using namespace std;
//b
string join(string first,string last)//it takes two strings first and last, join them...with space in between them
{
string full= first+" "+last;//string full...joined first and last..
return full;//returning full string..
}
//c
//method that takes one string and prints it in reverse order...
//doesn't returns anything
void reverse(string x)
{
int n =x.size();//storing size in n
n--;
//printing string in reverse order
while(n>=0)
{
//displaying one character at a time...
cout<<x[n];
n--;
}
cout<<" ";
}
//testing method
int main()
{
string first,last;
//taking input..first and last
cout<<"Enter string1 to join:";
cin>>first;
cout<<"Enter string2 to join:";
cin>>last;
//joining first and last...by calling function join
//storing result in full
string full =join(first,last);
//printing result..
cout<<"After joining :"<<full<<endl;
//reading string to reverse..
string str;
cout<<"Enter string to print reverse order:";
cin>>str;
cout<<"Reverse order :";//printing result..
reverse(str);
return 0;
}
output:
Enter string1 to join:hello
Enter string2 to join:world!
After joining :hello world!
Enter string to print reverse order:Surya
Reverse order :ayruS
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.