Question

The funciton my_strcpy(char dest, const char cource) is to copy "hello world" from source to dest....

The funciton my_strcpy(char dest, const char cource) is to copy "hello world" from source to dest. Do not use the library. The copy process will only stop at the last "\0". Make sure dest is large enough to any lenght of sentence inputted. The following is my code. The program copy the first letter of "hello world" which is "h", but the requirement is to copy the whole sentence "hello world". Thank you for your helps

#include <iostream>

using namespace std;

void my_strcpy(char dest[],const char source[]) {

int i = 0;

while (source[i] != '\0') {

dest[i] = source[i];

++i; }

dest[i] = '\0';

}

int main( ){

char dest;

char source[]="hello world";

my_strcpy(&dest,source);

cout<<"Destination now : "<< dest << endl;

return 0;

}

Homework Answers

Answer #1

There is a very small logical error in the code provided here.

Characters in c++ are 1 byte long, character arrays are more than 1 byte.

So, to store "hello world" we need a character array and not just a character.

Thus we have to declare an array of characters big enoung in place of just a character to accomodate the complete string "hello world" like this, char dest[20];

Also now when passing the argument in the funtion we need to only write my_strcpy(dest,source); and not my_strcpy(&dest,source); because the name of an array points to the location of the first element of the array ie &dest[0], thus my_strcpy(&dest[0],source); will also work.

Here is the complete rectified code

//These are the required header file
#include <iostream>
using namespace std;

//No changes needed here
void my_strcpy(char dest[], const char source[])
{
    int i = 0;
    while (source[i] != '\0')
    {
        dest[i] = source[i];
        ++i;
    }
    dest[i] = '\0';
}

int main()
{
    //Making the destination array now so that we can store the string
    char dest[20];
    char source[] = "hello world";
    //Passing the argument as dest in place of &dest
    //my_strcpy(&dest[0], source); will also work fine, but it is lengthy
    my_strcpy(dest, source);
    //Now this will show the correct output
    cout << "Destination now : " << dest << endl;
    return 0;
}

Here is the image of the code to help understand indentation

Here is the output

NOTE: If you liked the answer a thumbs up would make my day :) Have a nice day.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please fill in the blank bolded below that would be the best choice for this program....
Please fill in the blank bolded below that would be the best choice for this program. #include <iostream> using namespace std; const int size = 100000; class TheBig { public: double operator[](int index) const {return (theData[index]);} private: double theData[size]; }; void firstToBeThe( ______________________________________________________) { for (int i = 0; i <size; i++) cout << value[i] <<endl; } int main() { TheBig one; firstToBeThe(one); }
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
C++ PROGRAM When I input 3 S P R, it was suppoesed to pop up L...
C++ PROGRAM When I input 3 S P R, it was suppoesed to pop up L W T. But it showed L L L.IDK why the moveNo is not working. I am asking for help, plz dont put some random things on it. main.cpp #include <iostream> #include "computer.h" #include "human.h" #include "referee.h" using namespace std; int main() {     human h;     computer c;     referee r;     r.compare(h,c);     return 0; } computer.cpp #include<iostream> #include "computer.h" using namespace std;...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
read a text file into two parrall arrays. The file has a name and a grade...
read a text file into two parrall arrays. The file has a name and a grade here is what I am doing not working #include < iostream> #include < fstream> #include <string> usind namespace std; int main() { int const size =2; string names[size]; int age[size]; ifstream inputFile; inputFile.open("text.txt"); for (int i = 0; i <size; i++) { inputFile >> names[i]; inputFile>> age[i]; cout << age[i] << endl; cout << name[i] << endl; } inputFile.close(); return 0; }
Utilize the code from last week Add a default, full, and copy constructor. Also add a...
Utilize the code from last week Add a default, full, and copy constructor. Also add a constructor that allows you to specify only the name of the video game with no high score or times played specified. Adjust your code to demonstrate use of all 4 constructors and output of the resulting objects. #include <iostream> #include <string> #include <iomanip> using namespace std; class VideoGame { private:     string name;     int highScore;     int numOfPlays; public:     VideoGame() {        ...
Convert this C++ to JavaScript and run in browser. #include <cstdlib> #include <ctime> #include <sstream> #include...
Convert this C++ to JavaScript and run in browser. #include <cstdlib> #include <ctime> #include <sstream> #include <iostream> using namespace std; /* * */ class Dice{ private: static const int MAXDICE=6; static const int MINDICE=1; int faceVal; public: Dice(int); void setFace(int); int getFace(); string toString(); }; Dice::Dice(int faceVal) { if(faceVal<MINDICE&&faceVal>MAXDICE) { setFace(1); } else { this->faceVal=faceVal; } } void Dice::setFace(int faceVal) { this->faceVal=faceVal; } int Dice::getFace() { return faceVal; } string Dice::toString() { stringstream ss; ss<<faceVal; string str="face value is ";...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
Experience with Functions Are the following statements a function header, a prototype or a function call?...
Experience with Functions Are the following statements a function header, a prototype or a function call? double calcMonthlyPmt(double amt); void displayResults() void showMenu(); showNum(45.67); area = CalArea(5,10); Write the function calls for the following headers and prototypes: void showValue(int quantity) void display(int arg1, double arg2, char arg3); pass the following to the call: int age; double income; char initial double calPay(int hours, double rate); What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT