C语言字符串重排代码本地正常,Eagle平台输出异常求助
C语言字符串拼接代码在线评测平台输出异常问题
我编写了一段C语言代码,功能如下:
- 输入字符串
s、整数l和r - 将
s拆分为三部分:- 起始至
l-1位 l至r-1位r至末尾
- 起始至
- 按3、2、1的顺序拼接成
temp字符串输出 - 输入
quit时终止循环
本地测试一切正常,但上传至学校评分平台Eagle后,输出出现异常:第三部分单独显示,随后是第二部分加第一部分的内容,无法定位问题。
代码如下:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int l, r, n, i; char s[1000]; fgets(s, 1000, stdin); s[strcspn(s, "\n")] = '\0'; while (strcmp(s, "quit") != 0) { scanf("%d %d", &l, &r); n = strlen(s); if (l < 0 || r >= n || l > r) { while (getchar() != '\n'); continue; } else { char *temp = (char *)malloc((n + 1) * sizeof(char)); if (temp == NULL) { return 1; } int j = 0; //printf("Copying Segment 1 (r..n): "); for (i = r; i < n; i++) { temp[j++] = s[i]; //printf("%c", s[i]); } //printf("\n"); //printf("Copying Segment 2 (l..r): "); for (i = l; i < r; i++) { temp[j++] = s[i]; //printf("%c", s[i]); } //printf("\n"); //printf("Copying Segment 3 (0..l): "); for (i = 0; i < l; i++) { temp[j++] = s[i]; //printf("%c", s[i]); } //printf("\n"); temp[j] = '\0'; printf("%s\n", temp); fflush(stdout); free(temp); } while (getchar() != '\n'); fgets(s, 1000, stdin); s[strcspn(s, "\n")] = '\0'; } return 0; }
Eagle平台输出异常截图:

内容的提问来源于stack exchange,提问作者Mr BaW
相关产品推荐
相关产品推荐

