0

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

3 Answers3

1
 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] << " ";
        }
    }
Hitokiri
  • 3,607
  • 1
  • 9
  • 29
1

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>,

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Even if your answer is a bit better, I've chosen the other one as the correct one because it was faster. Can you elaborate on using "cstring" instead of "string.h"? As far as I know, cstring should be avoided (because it's obsolete). – Octavian Niculescu Apr 29 '20 at 15:58
  • @JohnSmith It is a requirement of the C++ Standard. – Vlad from Moscow Apr 29 '20 at 15:59
  • @JohnSmith -- `` puts the names of the C functions that are declared in `` into the namespace `std::`. It is not obsolete, and there is no reason to avoid it. There were some folks early on who thought the the entire C library should be tolerated until it could be removed. That effort quickly failed. – Pete Becker Apr 29 '20 at 16:08
0

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]).

Pete Becker
  • 74,985
  • 8
  • 76
  • 165