-3

我的练习有问题,所以这里是练习:

编写一个程序,通过屏幕上的适当消息来帮助读取两个字符串str1str2甚至是从键盘给出的字符串,然后删除变量中的所有字母,这些字母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");

}

所以有人可以帮助我吗?

4

3 回答 3

2
     if (str2[(strlen(str2)-j)]=str1[(strlen(str1)-i)])
         str1[(strlen(str1)-i)]=' ';

When iis 0thenstr2[(strlen(str1)]是您要覆盖的字符串的终止空字符' '。你需要一个- 1地方。

正如@PaulR在评论中指出的那样,第一个=应该是==.

另请注意,您必须使用scanf("%s", str)而不是scanf("%s", &str).

于 2012-06-02T12:40:00.587 回答
1
#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");

}
于 2012-06-02T18:50:25.240 回答
-1

您的问题是关于 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);
于 2012-06-02T12:44:59.183 回答