大佬救救,为什么gets过不了scanf就行
查看原帖
大佬救救,为什么gets过不了scanf就行
1108085
losyi楼主2023/11/21 23:11
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<stdlib.h>
//1 str:后接插入,在文档后面插入字符串 strstr,并输出文档的字符串;
//2 a b:截取文档部分,只保留文档中从第a个字符起b个字符并输出文档的字符串;
//3 a str:插入片段,在文档中第a个字符处插入字符串str并输出文档的字符串;
//4 str:查找子串,查找字符串str在文档中最先的位置并输出;如果找不到输出−1。
int main()
{
    int n;
    scanf("%d ", &n);
    char s[1000] = { 0 };
    gets(s);
    for (int i = 0; i < n; i++)
    {
        int choice = 0;
        scanf("%d ", &choice);
        switch (choice)
        {
        case 1: {
            char a[100];
            gets(a);
            strcat(s, a);
            printf("%s\n", s);
            break;
        }
        case 2: { int x1, y;
            scanf("%d %d", &x1, &y);
            s[x1 + y] = '\0';
            strcpy(s, &s[x1]);
            printf("%s\n", s);
            break;
        }
        case 3: {
            int x2; scanf("%d ", &x2);
            char b[100];
            gets(b);
            strcat(b, &s[x2]);
            s[x2] = '\0';
            strcat(s, b);
            printf("%s\n", s);
            break;
        }
        case 4: {
            char d[1000];
            gets(d);

            char* p = strstr(s, d);
            if (p != NULL)
            {
                printf("%d\n", p - s);
            }
            else
            {
                printf("-1\n");
            }
            break;
        }
        }
    }
    return 0;
}

前三个测试过不了,但本地测试结果正常,换成scanf读取字符串就能过了,

2023/11/21 23:11
加载中...