«

【C语言】教材5.1 字符串拼接

azurekiln 发布于 阅读:34 C程序设计


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    int i=1, max=100, sum=0;
    char result[1000] = "";
    char buffer[10];
    for (i=1; i<=max; i++) {
      sum=sum+i;

      snprintf(buffer, sizeof(buffer), "%d", i);
      if (i==max) {
        strcat(result, buffer);
        strcat(result, "=");
      } else {
        strcat(result, buffer);
        strcat(result, "+");
      }
    }
    printf("%s%d",result,sum);
    return 0;
}


扫描二维码,在手机上阅读
收藏
请先 登录 再评论