Hello, I need help with my this program. I have to define the 4 functions #inclu
ID: 3836774 • Letter: H
Question
Hello, I need help with my this program. I have to define the 4 functions
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <sstream>
using namespace std;
string int2cpp (int); // 1. convert int to C++ string
void int2c (int, char[]); // 2. convert int to C-string
void cpp2c (string, char[]); // 3. convert C++ string to C-string
string c2cpp (char[]); // 4. convert C-string to C++ string
int cpp2int (string); // 5. convert C++ string to int
int c2int (char[]); // 6. convert C-string to int
// note: in the functions below, the first argument is the 'from'
// data structure and the second is the 'to'
void c2t (char[], char[]); // 7. convert C-string to char array w/o term
void t2c (char[], char[]); // 8. convert char array w/o term to C-string
int main(int argc, char * argv[])
{
string s1("1234");
string s3;
string s4;
char xy[10] = "567";
char qq[10];
char ca1[10];
char ca2[10];
char ct1[10];
char ct2[10];
int j;
int k;
// You will get credit if your code WORKS in the GENERAL CASE (i.e., not
// just for these specific examples) and does NOT use any character by
// character processing. (You can update ONE character; you just can't
// update an entire string one character at a time.)
// If you write the code for pratice, make sure to add some print statements
// in the driver so that you know your code works.
// Make sure your code does what is required, i.e., updates the correct
// argument or returns a value of the correct type. Notice that none of
// these functions asks you to print anything.
s4 = int2cpp(42); // #1
cout << s4 << endl;
int2c(42, ca1); // #2
cout << cal << endl;
/* cpp2c(s1, qq); // #3
s3 = c2cpp(xy); // #4
j = cpp2int(s1); // #5
k = c2int(xy); // #6
strncpy(ct2, xy, 2);
c2t(xy, ct1); // #7
t2c(ct2, ca2); // #8
*/
return 0;
}
// 1. Convert int to C++ string.
string int2cpp (int i)
{
char* ch = itoa(i);
string str = string(ch);
return str;
}
Explanation / Answer
Please find the functions below. Check from your program if it is of help to you.
void int2c (int num, char[] value){
sprintf(value, "%d", num);
}
void cpp2c (string inp, char[] output){
std::copy(inp.begin(), inp.end(), output);
writable[inp.size()] = ''; // don't forget the terminating 0
}
string c2cpp (char[] input){
std::string outputString(input);
return outputString;
}
int cpp2int (string input){
std::string::size_type sz; // alias of size_t
return std::stoi (input,&sz);
}
int c2int (char[] input){
int num = (int) strtol(input, (char **)NULL, 10);
return num;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.