implicit declaration of function ‘strrev’

19,309

Solution 1

If you are compiling in Linux, then strrev() is not part of string.h. The linked question contains an answer linking to source for an implementation you can bring into your own code directly.

Solution 2

char *strrev(char *str){
    char c, *front, *back;
    if(!str || !*str)
        return str;
    for(front=str,back=str+strlen(str)-1;front < back;front++,back--){
        c=*front;*front=*back;*back=c;
    }
    return str;
}

Solution 3

Incase some of the other peices of code are hard to read or you do not know how to use pointers, here is a short script to reverse a string.

#include <stdio.h>
#include <string.h>
int main(void)
{
// Start here
char String1[30] = "Hello",reversed[30];
int i;
long int length = strlen(String1);
//Reverse string
for(i=0; i<length; ++i)
{
    reversed[length-i-1] = String1[i];
}
printf("First string %s\n",String1);
printf("Reversed string %s\n",reversed);
// To here if you want to copy and paste into your code without making functions 
return 0;
}
Share:
19,309

Related videos on Youtube

Sean O'Brien
Author by

Sean O'Brien

Updated on June 04, 2022

Comments

  • Sean O'Brien
    Sean O'Brien 6 months

    Any ideas on why when I try to compile this code to check if a line from the file atadata I get the warning:

    warning: implicit declaration of function ‘strrev’ [-Wimplicit-function-declaration]

    CODE

    #include <stdio.h>
    #include <string.h>
    int main(){
        char palindromes[100];
        char reverse[100];
        FILE *atadata = fopen("atadata","r");
        while (fgets(palindromes,sizeof(palindromes),atadata) != NULL){
            strcpy(reverse,palindromes);
            strrev(reverse);
            if( strcmp(atadata,reverse) == 0)
            fputs(atadata, stdout);
        }
        fclose(atadata);
        return 0;
    }
    
    • Martin James
      Martin James about 8 years
      Is there a definition for it in string.h?
    • David C. Rankin
      David C. Rankin about 8 years
      There is no strrev unless you have written one. (then you need to include the header with your declaration)
    • The Paramagnetic Croissant
      The Paramagnetic Croissant
      @SeanO'Brien You open a text editor, type in the necessary code, save it as a text file and compile it using a C compiler.
  • Manjunath N
    Manjunath N about 8 years
    This link might be useful from Stack overflow stackoverflow.com/questions/8534274/…
  • Alex Reynolds
    Alex Reynolds about 8 years
    That's the same link that is in my answer.
  • Manjunath N
    Manjunath N about 8 years
    Oh! i didn't observe that's good solution actually.
  • pasignature
    pasignature over 2 years
    This is punk of readable coding style. I love simplicity.
  • Harshit Gangwar
    Harshit Gangwar 9 months
    Works for me. !

Related