Compare strings in C exercise

2

I have the following exercise:

Implement a function that is passed as input to two character strings. The function must return a 1 if the strings are equal, but it will return a 0 (it can not use specific functions of C).

This is what I tried:

#include<stdio.h>

 char cadenas (char cad1[100],char cad2[100] ) {


int i;


 for (i=0;i<100;i++){

    printf ("\nintroduzca el caracter %s de la cadena");
        scanf  ("%s", cad1[i]);

}


 for (i=0;i<100;i++) {

   printf ("\nintroduzca el caracter %s de la cadena");
    scanf ("%s",cad2[i]);

}


 if (cad1==cad2) {

   printf ("las cadenas %s son iguales");

      return 1;

What happens is that when compiling I get several warnings, I do not know what errors the code could have:

ejexamen.c: In function ‘cadenas’:
ejexamen.c:22:11: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
   printf ("\nintroduzca el caracter %s de la cadena");
           ^
ejexamen.c:23:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
    scanf  ("%s", cad1[i]);
            ^
ejexamen.c:30:12: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
    printf ("\nintroduzca el caracter %s de la cadena");
            ^
ejexamen.c:31:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
   scanf ("%s",cad2[i]);
          ^
ejexamen.c:38:12: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
    printf ("las cadenas %s son iguales");
            ^
ejexamen.c:42:8: error: expected ‘}’ before ‘else’
        else { 
        ^

Thanks

    
asked by Robert 25.03.2017 в 00:14
source

1 answer

3

Focusing only on the code, without looking at the messages:

printf ("\nintroduzca el caracter %s de la cadena");

You indicate that you want to show a string ('% s'), but do not pass it as an argument to the function . It should be

printf( "\nintroduzca el caracter %d de la cadena", i );

to show the index (which I think is what you want).

scanf  ("%s", cad1[i]);

There, you ask for a string , but you pass it as a argument . It should be:

scanf( "%c", &cad1[i] );

and you will get a curious behavior when executing it, but I leave it for you to discover it; -)

All the above happens again, in the next piece of code.

Finally, you do

if (cad1==cad2)

That will give you false always . What you are doing is comparing pointers to strings, which is not what you want.

To compare the content of the chains, the easiest thing I can think of is

while( ( *cad1 && *cad2 ) && ( *cad1 == *cad2 ) ) {
  ++cad1;
  ++cad2;
}

if( *cad1 == *cad2 ) {
  // Son iguales.
  ...
} else {
  // Son distintas.
  ...
}

And finally, I would change the definition of the function by something like this:

int cadenas( char *cad1, char *cad2 ) {

mainly, for clarity. It does not really affect.

Ah, I forgot. You have 2 } left at the end of your code. I guess you have not copied / pasted it well.

    
answered by 25.03.2017 в 00:32