C 比较两个字符串并删除常用字母

C 比较两个字符串并删除常用字母,c,string,compare,character,C,String,Compare,Character,我对一个练习有问题,所以练习如下: 编写一个程序,帮助在屏幕上显示适当的消息,以读取两个字符串str1和str2(即使是从键盘上给出的字符串),然后删除变量str1中的所有字母,这些字母也会出现在变量str2中。显示屏显示检查程序正确运行的最终结果 以下是我迄今为止所做的工作,我只能使用这些库: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char s

我对一个练习有问题,所以练习如下:

编写一个程序,帮助在屏幕上显示适当的消息,以读取两个字符串str1和str2(即使是从键盘上给出的字符串),然后删除变量str1中的所有字母,这些字母也会出现在变量str2中。显示屏显示检查程序正确运行的最终结果

以下是我迄今为止所做的工作,我只能使用这些库:

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

int main()
{
    char str1[80], str2[80];
    int megethos1, megethos2,max,i,j;
    printf ("Give the first string: ");
    scanf ("%s", &str1);
    printf ("Give the second string: ");
    scanf ("%s", &str2);

    size1= strlen(str1);
    size2= strlen(str2);

    for (j=0; j<=megethos2; j++){
        for (i=0; i<=megethos1; i++){
             if (str2[(strlen(str2)-j)]=str1[(strlen(str1)-i)])
                 str1[(strlen(str1)-i)]=' ';
        }
    }

    printf (str1);

    system("pause");

}
有人能帮我吗

     if (str2[(strlen(str2)-j)]=str1[(strlen(str1)-i)])
         str1[(strlen(str1)-i)]=' ';
当i为0时,str2[strlenstr1]是要用“”覆盖的字符串的终止空字符。你需要一个-1的地方

如注释中所述,第一个=应该是a==


还要注意,您必须使用scanf%s、str,而不是scanf%s和str。

您的问题是关于C还是C

对于C,您可以使用LINQ:

var s1 = "string new";
var s2 = "string";

var excludedCharText =
  s1
    .Where(c => s2.All(o => o != c))
    .Select(c => c.ToString())
    .Aggregate((prev, next) => prev + next);

到目前为止,您编写的代码有问题吗?它不起作用吗?它有什么行为?你用的是c,不是c。而且,标签不属于标题,我用大写字母写的。我知道你已经了解了斯特伦。标准库中还有许多其他字符串处理函数——特别是,我建议您阅读strpbrk、strspn、strcspn和strchr的文档。当你在那里的时候,四处寻找其他有趣的功能,这些功能可能会帮助你完成以后的任务。祝你好运。还要注意的是,第一个=应该是a=?我认为你的整个答案是一个非常慢的新字符串1.Excepts2.ToArray的替代品,它已经非常低效了。如果您想要更高效的东西,请查看字符串生成器
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char str1[80], str2[80];
    int size1,size2, i,j;
    printf ("Give the first string: ");
    scanf ("%s", str1);
    printf ("Give the second string: ");
    scanf ("%s", str2);

    size1= strlen(str1);
    size2= strlen(str2);

    for (j=0; j<size2; j++){
        for (i=0; i<size1; i++){
             if (str2[j]==str1[i])
                 str1[i]=' ';
        }
    }

    printf("%s\n", str1);

    system("pause");

}