简体   繁体   中英

Strcmp doesn't work and I don't seem to understand why - transformation to ASCII code makes the program work as expected though

First of all, please don't critique the way the program is written, because this is what we study in my country.

I know it is a mixture of C and C++ and the things I use are outdated, but that's how the things are here.

So I have to make a program which gets as an input n words. Then I have to print the words that have the last one as a prefix.

eg

input: 
n=6 
raita grai raid raion straie rai
output:
raita raid raion

This is my program. It works as expected:

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    int n;
    char a[100][100];
    bool ok=1;
    cin >> n;
    cin.get();
    for (int i = 0; i < n; i++)
    {
        cin.get(a[i], 100);
        cin.get();
    }
    int p = strlen(a[n - 1]);
    for (int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < p; j++)
        {
            ok = 1;
            if ((unsigned int)a[i][j] != (unsigned int)a[n-1][j])
            {
                ok = 0;
                break;
            }
        }
        if (ok == 1)
        {
            cout << a[i] << " ";
        }
    }
}

But initially, it looked like this:

/* strstr example */
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
    int n;
    char a[100][100];
    bool ok=1;
    cin >> n;
    cin.get();
    for (int i = 0; i < n; i++)
    {
        cin.get(a[i], 100);
        cin.get();
    }
    int p = strlen(a[n - 1]);
    for (int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < p; j++)
        {
            ok = 1;
            if (strcmp(a[i][j], a[n-1][j]) != 0)
            {
                ok = 0;
                break;
            }
        }
        if (ok == 1)
        {
            cout << a[i] << " ";
        }
    }
}

and it throws some errors:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0167   argument of type "char" is incompatible with parameter of type "const char *"   ConsoleApplication1 25  

Severity    Code    Description Project File    Line    Suppression State
Error   C2664   'int strcmp(const char *,const char *)': cannot convert argument 1 from 'char' to 'const char *'    ConsoleApplication1     25  

I cannot seem to understand why this happens. Can any of you help me understand? Also, should I be using conversion to (unsigned int) or just strcmp?

Thanks.

 int strcmp(const char *s1, const char *s2);

strcmp used to compare string and string . But in your code, you compare char and char ( a[i][j] and a[n-1][j] ).

In your case, you can use strncmp that compares only the first (at most) n bytes (in your case, n is strlen(a[n-1]) ) of two strings:

int strncmp(const char *s1, const char *s2, size_t n);

So, your program becomes as below:

    for (int i = 0; i < n - 1; i++)
    {
        ok = 1;
        if (strncmp(a[i], a[n-1], p) != 0)
        {
            ok = 0;
        }
        if (ok == 1)
        {
            cout << a[i] << " ";
        }
    }

In this statement

if (strcmp(a[i][j], a[n-1][j]) != 0)

the both expressions a[i][j] and a[n-1][j] have the type char while the function strcmp expects two pointers to strings of the type char * .

So the compiler issues an error.

You could simplify your first program using the standard function strncmp . For example

size_t p = strlen(a[n - 1]);
for (int i = 0; i < n - 1; i++)
{
    if ( strncmp( a[i], a[n-1], p ) == 0 ) cout << a[i] << " ";
}

Pay attention to that you should use the header <cstring> instead of the header <string.h> ,

The simplest change to the code in the question is to change the test from if (strcmp(a[i][j], a[n-1][j]) != 0) to if (a[i][j] != a[n-1][j]) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM