之前的解答有问题,哪位人士帮一下我,求急!

从键盘输入字符串1和字符串2(串长不超过50个字符),将在字符串1中出现,但未在字符串2中出现的字符组成一个新的字符串输出,不去掉未出现过的重复字符。

基于new bing的参考学习:

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

#define MAX_LEN 50

int main()
{
    char str1[MAX_LEN+1], str2[MAX_LEN+1], result[MAX_LEN+1];
    int i, j, k, flag;

    printf("请输入字符串1:");
    gets(str1);

    printf("请输入字符串2:");
    gets(str2);

    k = 0; // 记录新字符串result的长度
    for (i = 0; i < strlen(str1); i++) {
        flag = 0; // 初始化为未出现过
        for (j = 0; j < strlen(str2); j++) {
            if (str1[i] == str2[j]) {
                flag = 1; // 出现过
                break;
            }
        }
        if (!flag) { // 如果在str2中未出现过
            result[k++] = str1[i]; // 存入result中
        }
    }
    // 添加结束符
    result[k] = '\0';

    printf("新的字符串为:%s\n", result);

    return 0;
}


  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7584391
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:写一个函数,将一个字符串中的元音字母复制到另一字符串,然后输出
  • 除此之外, 这篇博客: 一些零碎代码中的 输入字符串,调用函数取出字符串中最长的单词并存入另一个字符串。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • (有待思考)#define _CRT_SECURE_NO_WARNINGS
    #include
    #include
    #includeusing namespace std;
    void index(char* s1, char* s2)
    {
    int len ,max = 0;
    char temp[50];
    while (*s1)
    {
    while (*s1 == ’ ’ && *s1 != ‘\0’) s1++;//过滤空格;

    len = 0;
    while (*s1 != ' ' && *s1 != '\0')
    {    
        *(temp + len) = *s1;//不能用*temp=*s1,why?
        len++;
        s1++;
    }
    
    *(temp + len) = '\0';//注意这种方式。
    if (len > max)
    {
        max = len;
        strcpy(s2, temp);
    }
    
    if (*s1 == '\0') break;
    

    }}
    int main()
    {
    char s1[50],s2[50];cin.get(s1,50); index(s1, s2);
    ​ cout << “s2:” << s2;
    }用队列的方法输出杨辉三角:#include
    using namespace std;
    const int maxsize = 100;
    typedef struct {
    int Q[maxsize];//存放数据
    int front, rear;
    }sequeue;
    sequeue qu;
    void setkong(sequeue& qq)
    {
    qq.front = 0;
    qq.rear = 0;
    }//置队空
    void rudui(sequeue& qq, int x)
    {
    if (qq.front == (qq.rear + 1) % maxsize)
    cout << “overflow\n”;
    else
    {
    qq.Q[qq.rear] = x;
    qq.rear = (qq.rear + 1) % maxsize;
    }
    }
    void chudui(sequeue &qq, int& x)
    {
    if (qq.front == qq.rear)
    {
    cout << “underflow\n”;
    }
    else
    {
    x = qq.Q[qq.front];
    qq.front = (qq.front + 1) % maxsize;
    }
    }
    void getfront(sequeue qq, int &x)//读取队头元素
    {
    if (qq.front == qq.rear)
    {
    cout << “error!\n”;
    }

    else
    {
    x = qq.Q[qq.front];
    }
    }
    int empty(sequeue qq)//判断队列是否为空
    {
    if (qq.front == qq.rear)
    return 1;
    else
    return 0;
    }
    void yanghui(int n,sequeue qu)
    {
    int i, j,s,t;
    setkong(qu);
    rudui(qu, 1); rudui(qu, 1);
    cout << endl;
    cout.width(4); cout << 1;
    cout.width(4); cout << 1<<endl;
    for (i = 2; i <= n; i++)//生成并输出杨辉三角第i~n行的数据
    {
    rudui(qu, 1);
    cout.width(4); cout << 1;
    chudui(qu, s);
    for (j = 2; j <= i; j++)//处理第i行中间的各数据
    {
    chudui(qu, t);
    rudui(qu, s + t);
    cout.width(4); cout << s + t;
    s = t;

        }
        rudui(qu, 1);
        cout.width(4); cout << 1<<endl;
      
    }
    cout << endl;
    


    }
    int main()
    {
    int m;
    cin >> m;
    yanghui(m, qu);
    }

  • 您还可以看一下 孙玖祥老师的图解数据结构与算法课程中的 操作最后一个元素复杂度震荡问题的解决小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    针对这个问题,可以通过以下步骤解决:

    1. 定义两个变量,分别存储两个字符串,如string1string2,并使用fgets函数从键盘输入两个字符串:
    char string1[51], string2[51];
    printf("请输入字符串1:");
    fgets(string1, 51, stdin);
    printf("请输入字符串2:");
    fgets(string2, 51, stdin);
    
    1. 定义一个用来存储重复字符的字符串,如result,并将其全部初始化为0:
    char result[256] = {0};
    
    1. 使用两层循环遍历字符串1和字符串2,找出出现在字符串1中但未出现在字符串2中的字符:
    for(int i = 0; i < strlen(string1); i++) {
        for(int j = 0; j < strlen(string2); j++) {
            if(string1[i] == string2[j]) break;
            if(j == strlen(string2)-1) result[string1[i]] = 1;
        }
    }
    

    其中,result数组中存储的是字符是否出现过的信息,如果字符出现过,则将其对应的ASCII码位置上的值设为1。由于ASCII码共有256个,所以数组的大小设为256。

    1. 最后将result数组中值为1的字符全部输出,组成新的字符串:
    char new_string[256];
    int index = 0;
    for(int i = 0; i < 256; i++) {
        if(result[i] == 1) {
            new_string[index++] = (char)i;
        }
    }
    new_string[index] = '\0';
    printf("新字符串为:%s", new_string);
    

    完整的代码如下:

    ```c

    include

    include

    int main() {

    char string1[51], string2[51];
    printf("请输入字符串1:");
    fgets(string1, 51, stdin);
    printf("请输入字符串2:");
    fgets(string2, 51, stdin);
    
    char result[256] = {0};
    for(int i = 0; i < strlen(string1); i++) {
        for(int j = 0; j <