九度OJ-1471:合并符串

    xiaoxiao2021-03-25  63

    题目描述:

    给定两个字符串S1和S2,合并成一个新的字符串S。 合并规则为,S1的第一个字符为S的第一个字符,将S2的最后一个字符作为S的第二个字符; 将S1的第二个字符作为S的第三个字符,将S2的倒数第二个字符作为S的第四个字符,以此类推。

    输入:

    包含多组测试数据,每组测试数据包含两行,代表长度相等的两个字符串S1和S2(仅由小写字母组成,长度不超过100)。

    输出:

    合并后的新字符串S

    样例输入: abc def 样例输出: afbecd 来源: 2011年西北工业大学计算机研究生机试真题 #include <cstdio> #include <cstring> #define MAXSIZE 110 using namespace std; int main(){ int len,p,q; char str1[MAXSIZE],str2[MAXSIZE],str[MAXSIZE*2]; while (scanf("%s",str1)!=EOF){ scanf("%s",str2); //initiate len=strlen(str1); p=0; q=len-1; //process for (int i=0;i<len*2;i+=2){ str[i]=str1[p]; p++; str[i+1]=str2[q]; q--; } str[len*2]='\0'; //output printf("%s\n",str); } return true; }

    转载请注明原文地址: https://ju.6miu.com/read-36765.html

    最新回复(0)