【问题标题】:c check two strings for same charactersc 检查两个字符串是否有相同的字符
【发布时间】:2014-09-28 15:36:34
【问题描述】:

我刚开始学习 C 编程,为了练习,我找到了一项任务。首先我必须扫描两个字符串。然后我必须逐个字符地比较它们,如果有任何相同的字符,我必须打印出相同字符的数量。 它必须用指针来完成。所以让我有“boat”和“ship”,所以程序将返回 0。但如果它是“boat”和“soap”,它将返回 2。

这是我到目前为止所得到的,但是当我运行它时它给了我错误。我把错误放在 cmets 中。

提前感谢您的帮助。



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

int number_of_same_characters(char *, char *);

int main()
{
    char * first[100];
    char * second[100];
    int  result = 0;    

    printf("Enter first string\n");
    gets(*first);

    printf("Enter second string\n");
    gets(*second);

    result = number_of_same_characters(*first, *second);   
    printf("%d\n", result); 
    return 0;
}

int number_of_same_characters(char *p, char *q){ //i get this error here - error:  invalid type argument of unary ‘*’ (have ‘int’)
    int counter = 0;

        for(int j = 0; *p[j] != '\0' || *q[j] != '\0'; ++j){  //i get this error here -  error: invalid type argument of unary ‘*’ (have ‘int’)    
            if(strcmp(*p[j], *q[j])){       
                 ++counter;        
           }        
    }   
    return counter;
}

【问题讨论】:

  • char *first[100]; --> char first[100]; --> gets(first) --> number_of_same_characters(first, second) --> ...等
  • gets is evil。不要使用它。

标签: c string compare


【解决方案1】:

主要是你有很多额外的* 乱扔程序。变量声明应该是:

char first[100];
char second[100];

输入调用应该是

gets(first);
gets(second);

方法调用应该是:

result = number_of_same_characters(first, second);   

最后,for 循环中不应有任何取消引用。

for(int j = 0; p[j] != '\0' || q[j] != '\0'; ++j){     
    if(strcmp(p[j], q[j])){       
       ++counter;        
    }        
}

这会让你更接近,但仍然存在一些问题。作为提示,|| 运算符是可疑的,您不需要使用 strcmp

值得指出的是,gets() 是一个危险的函数,会导致缓冲区溢出。刚开始的时候可以用,但是不要让它成为习惯,千万不要在生产代码中使用它!

【讨论】:

    【解决方案2】:

    您错误地定义了字符数组,并且错误地使用了运算符 *。

    试试下面的

    #include <stdio.h>
    #include <string.h>
    
    #define N    100
    
    int number_of_same_characters( const char *, const char * );
    
    int main()
    {
        char first[N];
        char second[N];
        int  result = 0;    
        size_t n;
    
        printf( "Enter first string: ");
        fgets( first, N, stdin );
    
        n = strlen( first );
        if ( first[n - 1] == '\n' ) first[n - 1] = '\0';
    
        printf( "Enter second string: ");
        fgets( second, N, stdin );
    
        n = strlen( second );
        if ( second[n - 1] == '\n' ) second[n - 1] = '\0';
    
        result = number_of_same_characters( first, second );   
    
        printf( "%d\n", result );
    
        return 0;
    }
    
    int number_of_same_characters( const char *p, const char *q )
    {
        int counter = 0;
        int i;
    
        for( i = 0; p[i] != '\0' && q[i] != '\0'; ++i )
        {
            if ( p[i] == q[i] ) ++counter;        
        }
    
        return counter;
    }
    

    如果输入船和肥皂,那么输出将是

    2
    

    【讨论】:

    • @niko85 我有一个错字。应该有 for( i = 0; p[i] != '\0' && q[i] != '\0'; ++i )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 2016-04-10
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多