Search code examples
c++for-loopc-stringsstrncmpfloppy

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.

e.g.

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.


Solution

  •  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] << " ";
            }
        }