append.cpp

来自「BigC++的源码」· C++ 代码 · 共 36 行

CPP
36
字号
#include <iostream>

using namespace std;

/**
   Appends as much as possible from a string to another string
   @param s the string to which t is appended
   @param s_maxlength the maximum length of s (not counting '\0')
   @param t the string to append
*/             
void append(char s[], int s_maxlength, const char t[])
{  
   int i = strlen(s);
   int j = 0;
   /* append t to s */
   while (t[j] != '\0' && i < s_maxlength)
   {  
      s[i] = t[j];
      i++;
      j++;
   }
   /* add zero terminator */
   s[i] = '\0';
}

int main()
{  
   const int GREETING_MAXLENGTH = 10;
   char greeting[GREETING_MAXLENGTH + 1] = "Hello";
   char t[] = ", World!";
   append(greeting, GREETING_MAXLENGTH, t);
   cout << greeting << "\n";
   return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?