搜档网
当前位置:搜档网 › c语言程序字符串连接

c语言程序字符串连接

c语言程序字符串连接
c语言程序字符串连接

c语言程序字符串连接

字符串连接将s2连接在s1后面

#include "stdio.h"

#include "string.h"

#include "math.h"

void main()

{

char s1[100],s2[30];

int i,j;

gets(s1);

gets(s2);

i=j=0;

while(s1[i]!='\0')

i++;

while(s2[j]!='\0')

{s1[i]=s2[j];

i++;

j++;}

s1[i]='\0';

printf("new string s1 is:");

puts(s1);

}

分析:需要理解并掌握字符串的输入输出函数gets()和puts(),以及零字符的使用方法。此程序在输入两个字符串之后,字符串2会连接在字符串1的后面。程序执行时输入字符'\0' 无法被识别,只有程序本身的字符串里出现零字符才会被识别,因此字符串2会完全连接在字符串1后面。

相关主题