编写C语言程序,要求输入s1:abcdefg s2:1324 屏幕输出s1:abcdefg s2:4231 s3:a4b2c3d1efg

如题所述

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

int main(void)
{
char *szStr, szS1[256] = {0}, szS2[256] = {0}, cTemp;
int iStrFrist, iStrEnd;

printf("输入字符串S1:");
scanf("%s", szS1);
printf("输入字符串S2:");
scanf("%s", szS2);
if ((szStr = (char*)malloc(strlen(szS1) + strlen(szS2))) == NULL)
{
printf("%s : malloc error: %s\n", __func__, strerror(errno));
return 0;
}
memset(szStr, 0, sizeof(szStr));
for(iStrFrist = 0; szS1[iStrFrist] != '\0' || szS2[iStrFrist] != '\0'; ++iStrFrist)
{
if (szS1[iStrFrist] != '\0')
sprintf(szStr, "%s%c", szStr, szS1[iStrFrist]);
if (szS2[iStrFrist] != '\0')
sprintf(szStr, "%s%c", szStr, szS2[iStrFrist]);
}

for(iStrFrist = 0, iStrEnd = strlen(szS1) - 1; iStrFrist < iStrEnd; ++iStrFrist, --iStrEnd)
{
cTemp = szS1[iStrFrist];
szS1[iStrFrist] = szS1[iStrEnd];
szS1[iStrEnd] = cTemp;
}
for(iStrFrist = 0, iStrEnd = strlen(szS2) - 1; iStrFrist < iStrEnd; ++iStrFrist, --iStrEnd)
{
cTemp = szS2[iStrFrist];
szS2[iStrFrist] = szS2[iStrEnd];
szS2[iStrEnd] = cTemp;
}
printf("s1=%s\ns2=%s\ns3=%s\n", szS1, szS2, szStr);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答