简体   繁体   English

帮助字符串 Arrays C++

[英]Help with String Arrays C++

#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.当我们处理文本时,为什么需要偏移,为什么要添加 1 和 2 的偏移。 I cannot seem to grasp strings and Arrays, anyone mind helping?我似乎无法掌握字符串和 Arrays,有人介意帮忙吗?

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.偏移量用于跟踪数组中字符串的当前 position,以便您可以将新参数strcpy到正确的位置。

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.您将分隔符(空格)的长度添加到偏移量,以便将名称的下一部分写入正确的位置。

"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.随着更多字符被复制到全名中,偏移量增加以指向全名数组中剩余未使用空间的第一个字符。

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.将偏移量设置为等于 firstname 的长度,以便您知道从哪里开始将下一个内容复制到全名缓冲区中,在本例中为空格字符。

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.复制姓氏。

Understand these two statements and then it is a pencil paper exercise.理解这两个陈述,然后这是一个铅笔纸练习。

  1. Array index starts from 0.数组索引从 0 开始。
  2. arrayVariable + integer is the index at the integer + 1 location in the arrayVariable sequence. arrayVariable + integerarrayVariable序列中integer + 1位置的索引。 So, arr+3 is the location 4 in the sequence.因此, arr+3是序列中的位置 4。 ( Since array index starts from 0. ) (因为数组索引从 0 开始。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM