To compare two chains you have to go through both characters by character. In addition, the sign "==" means identity, not equality. When it is a basic type what is on the side of the sentence does have the meaning of equality, with objects and arrays does not work well. That is, pal1==pal1
is true, pal1==pal2
, is false, because even if they have the same content, they are different variables. The solution to your problem would be:
char pal1[10],pal2[10];
int igualdad=0;
printf("Ingrese una palabra: "); scanf("%s",pal1);
printf("Ingrese otra palabra: "); scanf("%s",pal2);
for (int i=0;i<10;i++){
if(pal1[i]!=pal2[i])
igualdad=-1;
}
if(igualdad==0)
printf("Las palabras son identicas");
else
printf("Las palabras son distintas");
And all this last one could turn it into a procedure to which you pass both words in the following way:
public void compararPalabras(int longitud, char pal1[],char pal2[]){
for (int i=0; i<longitud ; i++)
{
if(pal1[i]!=pal2[i])
igualdad=-1;}
if(igualdad==0)
printf("Las palabras son identicas");
else
printf("Las palabras son distintas");
}
The way to call this method in your program would be:
char pal1[10],pal2[10];
printf("Ingrese una palabra: "); scanf("%s",pal1);
printf("Ingrese otra palabra: "); scanf("%s",pal2);
compararPalabras(10,pal1,pal2);