m0_74101096 2023-01-12 23:05 采纳率: 100%
浏览 78
已结题

关于#c语言#的问题:怎么改变输入方式呢,指针——子串逆置

怎么改变输入方式呢?我想要的输入方式“beautiful aut”,现在的输入方式是“beautiful(回车键)aut(回车键)”,
目的是输出“betuaiful”,实现子串逆置


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef char elementype;
typedef struct node
{
    elementype data;
    struct node *next;
} linkstr;

linkstr *invertsubstring(linkstr *s, linkstr *t)
{
    linkstr *begin, *p, *t1, *r, *q, *u, *pr;
    pr = s;
    p = s;
    t1 = t;
    while (p != NULL && t1 != NULL)
    {
        if (p->data == t1->data)
        {
            p = p->next;
            t1 = t1->next;
        }
        else
        {
            begin = pr;
            pr = pr->next;
            p = pr;
            t1 = t;
        }
    }

    if (t1 != NULL)
    {
        printf("主串中没有匹配的子串\n");
        return s;
    }
    else
    {
        q = begin->next;
        r = q->next;
        q->next = p;
        while (r != p)
        {
            u = r->next;
            r->next = q;
            q = r;
            r = u;
        }
        begin->next = q;
    }
    return s;

}

linkstr *crtlinked()
{
    linkstr *s, *p, *head;
    char ch;

    head = NULL;
    p = head;

    scanf("%c", &ch);
    while (ch != '\n')
    {
        s = (linkstr *) malloc(sizeof(linkstr));
        s->data = ch;
        if (head == NULL)
            head = s;
        else p->next = s;
        p = s;
        scanf("%c", &ch);
    }
    p->next = NULL;

    return head;
}

void print_link(linkstr *s)
{
    linkstr *v;
    v = s;

    while (v)
    {

        if(v->next)
            printf("%c", v->data);
        else
            printf("%c", v->data);
        v = v->next;
    }

}

void main()
{
    linkstr *s, *t, *m;
    s = (linkstr *) malloc(sizeof(linkstr));
    t = (linkstr *) malloc(sizeof(linkstr));

    printf("请输入字符串:\n");
    s = crtlinked();
    printf("请输入要逆置的字符串:\n");
    t = crtlinked();
    m = (linkstr *) malloc(sizeof(linkstr));
    m = invertsubstring(s, t);
    print_link(m);
}
  • 写回答

2条回答 默认 最新

  • qzjhjxj 2023-01-13 10:08
    关注

    改动处见注释,供参考:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    typedef char elementype;
    typedef struct node
    {
        elementype data;
        struct node* next;
    } linkstr;
    
    linkstr* invertsubstring(linkstr* s, linkstr* t)
    {
        linkstr* begin, * p, * t1, * r, * q, * u, * pr;
        pr = s;
        p = s;
        t1 = t;
        while (p != NULL && t1 != NULL)
        {
            if (p->data == t1->data)
            {
                p = p->next;
                t1 = t1->next;
            }
            else
            {
                begin = pr;
                pr = pr->next;
                p = pr;
                t1 = t;
            }
        }
    
        if (t1 != NULL)
        {
            printf("主串中没有匹配的子串\n");
            return s;
        }
        else
        {
            q = begin->next;
            r = q->next;
            q->next = p;
            while (r != p)
            {
                u = r->next;
                r->next = q;
                q = r;
                r = u;
            }
            begin->next = q;
        }
        return s;
    }
    
    linkstr* crtlinked(char *str) //修改
    {
        linkstr* s, * p, * head;
        int i = 0;//char ch;    //修改
    
        head = NULL;
        p = head;
    
                        //scanf("%c", &ch);//修改
        while (str[i])  //(ch != '\n')     //修改
        {
            s = (linkstr*)malloc(sizeof(linkstr));
            s->data = str[i];  //s->data = ch;
            if (head == NULL)
                head = s;
            else p->next = s;
            p = s;
            i++;       //scanf("%c", &ch); //修改
        }
        p->next = NULL;
        return head;
    }
    
    void print_link(linkstr* s)
    {
        linkstr* v;
        v = s;
        while (v)
        {
    
            if (v->next)
                printf("%c", v->data);
            else
                printf("%c", v->data);
            v = v->next;
        }
    }
    
    void main()
    {
        linkstr* s, * t, * m;
        char ss[64], s1[64];    //修改
        //s = (linkstr*)malloc(sizeof(linkstr)); //修改
        //t = (linkstr*)malloc(sizeof(linkstr)); //修改
    
        //printf("请输入字符串:\n");             //修改
        scanf("%s %s", ss, s1);                 //修改
        s = crtlinked(ss);                     //修改
        //printf("请输入要逆置的字符串:\n");   //修改
        t = crtlinked(s1);                     //修改 
        //m = (linkstr*)malloc(sizeof(linkstr));//修改
        m = invertsubstring(s, t);
        print_link(m);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月21日
  • 已采纳回答 1月13日
  • 创建了问题 1月12日

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)