You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言字符串重排代码本地正常,Eagle平台输出异常求助

C语言字符串拼接代码在线评测平台输出异常问题

我编写了一段C语言代码,功能如下:

  • 输入字符串s、整数l和r
  • 将s拆分为三部分:
    1. 起始至l-1位
    2. l至r-1位
    3. 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平台输出异常截图:

Eagle平台输出异常

内容的提问来源于stack exchange,提问作者Mr BaW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 03:55:17