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

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


#include 
#include 
#include 
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);
}

改动处见注释,供参考:

#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);
}

供参考!谢谢!

img

img

img

img

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

char *res(char *s0, char *s)
{
    char *p1 = strstr(s0, s);
    if (!p1)
    {
        puts(s0);
        return NULL;
    }

    int len1 = strlen(s);
    char *p2 = p1 + len1 - 1;
    while (s0 < p1)
    {
        putchar(*s0);
        s0++;
    }

    while (p2 >= p1)
    {
        putchar(*p2);
        p2--;
    }

    return p1 + len1;
}
int main(int argc, char *argv[])
{
    char s0[256], s[256];
    scanf("%255s", s0);
    scanf("%255s", s);

    char *p = res(s0, s);
    while (p)
    {
        p = res(p, s);
    }
    puts("");

    return 0;
}