0

I have this type of array: char ArrayPalavra[100][200];

And i'm feeding like that:

pchPalavra = strtok(palavras," ");

while (pchPalavra != NULL)
{
    tamanhoArrayPalavra++;
    strcpy(ArrayPalavra[i++], pchPalavra);
    pchPalavra = strtok (NULL, " ");
}

I'm writing this array with words, like "this", "is", "a", "test". The problem is, if i put that array in a for for comparison with a word, that dont match.

for(int i = 0; i < tamanhoArrayPalavra; i++)
{
    if("this" == ArrayPalavra[i])
    {
        printf("Work!");
    }
}

But in test, if i print the ArrayPalavra[i], they come with "this". why using iteration doenst work ? I'm using C language.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    You cannot use the equal sign `==` to compare strings use [strcmp](http://en.cppreference.com/w/c/string/byte/strcmp) – Seek Addo Jun 25 '17 at 22:13

2 Answers2

1

Use strcmp() for string comparison, like this:

#include <string.h> // include the header that provides the methods for strings

// 'ArrayPalavra[i]' should be NULL terminated!
if(strcmp("this", ArrayPalavra[i]) == 0)
{
    printf("Equal\n");
}

Do not forget that strings in C should be NULL terminated!

gsamaras
  • 71,951
  • 46
  • 188
  • 305
-1

== operator doesn't work in C for strings. It will compare the pointers' memory addresses, which will always be false. you need to import the strings.h header and then you do

strcmp("this", ArrayPalavra[i]);