0
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
  char firstname[] = "Alfred";
  char middlename [] = "E";
char lastname[] = "Neuman";
  char fullname [80];
  int offset=0;

  strcpy(fullname,firstname);
  offset = strlen(firstname);
  strcpy(fullname+offset," ");
  offset +=1;
  strcpy(fullname+offset,middlename);
  offset += strlen(middlename);
  strcpy(fullname+offset," . ");
  offset +=2;
  strcpy(fullname+offset,lastame);

  cout << firstname << "." << middlename << "." << lastname << endl;
  cout << "Fullname:" << fullname << endl;

  return 0;
}

Why is offset needed in this and why is the off set added by 1 and 2, when we are dealing with text. I cannot seem to grasp strings and Arrays, anyone mind helping?

0

5 Answers 5

3

That's because you're using the wrong tools.

std::string firstname = "Alfred";
std::string middlename = "E";
std::string lastname = "Numan";
std::string fullname = firstname + " " + middlename + " . " + lastname;

The offset is used to track the current position of the string in the array so that you can strcpy the new argument into the right place.

Sign up to request clarification or add additional context in comments.

Comments

0

You're adding the length of your separators (spaces) to the offset, so that you write the next part of the name in the correct location.

Comments

0

"offset" is being used to move the starting point for the copy operation to a point one character after the last character in the array. As more characters are copied into fullname, offset increases to point to the first character of the remaining unused space in the fullname array.

Comments

0
strcpy(fullname,firstname);

Copy the firstname into the fullname buffer.

offset = strlen(firstname);

Set the offset equal to the length of firstname so you know where to start copying the next thing into the fullname buffer, in this case an empty space character.

offset +=1;

Move your offset passed that character so you start copying into the fullname buffer from that point on.

strcpy(fullname+offset,middlename);

Copy the middle name into fullname buffer.

offset += strlen(middlename);

Add the length of middlename to the offset, again so the next copy operation starts right after that.

strcpy(fullname+offset," . ");

Copy in a '.' for the middle initial.

offset +=2;

Increase your offset to point right passed that.

strcpy(fullname+offset,lastame);

Copy in the last name.

Comments

0

Understand these two statements and then it is a pencil paper exercise.

  1. Array index starts from 0.
  2. arrayVariable + integer is the index at the integer + 1 location in the arrayVariable sequence. So, arr+3 is the location 4 in the sequence. ( Since array index starts from 0. )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.