c - strncpy 和 strcat 用法

标签 c string syntax

我的作业要求显示我的姓名,如下所示:“姓氏,名字”。姓然后 [逗号空格] 名字。同时不移动该名称后的其余文本。这是我的代码:

  char str1[11];
  char str2[3];
  char str3[16];
  strcpy (str1,fn);
  strcpy (str2,", ");
  strcpy (str3,ln);
  strncat (str1, str2, 14);
  strncat (str1, str3, 31);

我的老师说我做了他想要的,但他不喜欢我用了多少行代码,说我做了超出我需要的额外工作。

变量:ln = last name, fn = first name 我为 ', ' 逗号空格制作了 str2。

他要我做什么?

最佳答案

假设你知道字符串的长度,为什么不呢

char result[50];

sprintf(result,"%s, %s",ln, fn);

关于c - strncpy 和 strcat 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33622704/

相关文章:

c - C 语言家族的名称

string - 条件绑定(bind)的初始化程序必须具有 Optional 类型,而不是 HTTPCookieStorage 上的 'String'

c - WIN32和其他c字符串的区别

c - 在循环中使用 strlen 反转 C 中的字符串

c++ - 对类里面的成员访问培训感到困惑?

c - 遍历给出段错误的字符列表

c - 编写处理任何类型数组的函数

C : how Get OS name ( Ubuntu, Debian、Centos、.. ) 并且如果定义操作系统名称 = ubuntu

ruby-on-rails - rails 语法错误 : wrong number of arguments

c++ - C 喜欢数组语法?