0

Iam trying to reverse a string using the strrev function in ubuntu. But iam getting two errors which says , "warning: implicit declaration of function ‘strrev’; did you mean ‘strsep’? [-Wimplicit-function-declaration]" and another error " warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=].Can anyone please help me resolve this problem. I have not learn pointers yet, so is there another method?

 #include <stdio.h>
    #include <string.h>
    int main()
    {
      char str[10] = "madam";
      printf("the given string is %s\n",str);
      printf("after reversing :%s",strrev(str));
      return 0;
    }
Zinzin
  • 5
  • 6

2 Answers2

0

strrev() is not a standard C function although it is included in some implementations.

See here for a discussion and a copy-paste strrev() implementation.

If a function is not found, it's assumed to return int by default. That's the reason of your second error. Your code seems fine.

For common/better formatting, please indent with 4 spaces and place spaces after , etc., and add a bit more air between the lines. Here's what I would do:

#include <stdio.h>
#include <string.h>

int main()
{
    char str[10] = "madam";

    printf("the given string is %s\n", str);
    printf("after reversing: %s", strrev(str));

    return 0;
}
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
0

strrev isn’t a standard c function. Looks like your compiler doesn’t define a function with that name. That leads to the second warning about passing an int to printf. There’s a rule in c that says if a function is encountered undefined it’s assumed to return an int. In that spot in printf you need a string, but strrev isn’t defined, so the compiler assumes it returns int.

To fix this you’ll have to make a strrev function yourself (or copy one from online)

kill -9
  • 159
  • 1
  • 9