c语言字符串拼接的有哪些方法
在C语言中,字符串拼接有多种方法,以下是其中几种经常使用的方法:
char *strcat(char *dest, const char *src);
示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[] = " World!";
strcat(str1, str2);
printf("%s
", str1);
return 0;
}
输出结果为:Hello World!
int sprintf(char *str, const char *format, ...);
示例:
#include <stdio.h>
int main() {
char str[100];
char name[] = "Alice";
int age = 20;
sprintf(str, "My name is %s and I am %d years old.", name, age);
printf("%s
", str);
return 0;
}
输出结果为:My name is Alice and I am 20 years old.
#include <stdio.h>
int main() {
char str1[50] = "Hello";
char str2[] = " World!";
int i = 0, j = 0;
while (str1[i] != '