串结构练习——字符串连接
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
给定两个字符串string1和string2,将字符串string2连接在string1的后面,并将连接后的字符串输出。
连接后字符串长度不超过110。
Input
输入包含多组数据,每组测试数据包含两行,第一行代表string1,第二行代表string2。
Output
对于每组输入数据,对应输出连接后的字符串,每组输出占一行。
Example Input
123
654
abs
sfg
Example Output
123654
abssfg
Hint
#include<cstdio>
using namespace std;
int main()
{
char str1[500],str2[500];
while(scanf("%s %s",str1,str2)!=EOF)//!=EOF是判断是否都到文件结尾,否则死循环,超时
{
int i,j;
for(i=0;str1[i]!='\0';i++);
for(j=0;str2[j]!='\0';j++)
{
str1[i]=str2[j];
i++;
}
str1[i]='\0';
printf("%s\n",str1);
}
return 0;
}
借鉴其他中输入避免漏掉EOF超时:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void conat(char t[], char s1[], char s2[])
{
int i=0, k=0;
while(s1[i]!='\0')
t[k++]=s1[i++];
i=0;
while(s2[i]!='\0')
t[k++]=s2[i++];
t[k]='\0';
}
int main()
{
char t[111], s1[111], s2[111];
while(gets(s1)!=NULL)
{
gets(s2);
conat(t, s1, s2);
puts(t);
}
}