2

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;
}
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Sean O'Brien
  • 21
  • 1
  • 1
  • 2

4 Answers4

4

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.

Community
  • 1
  • 1
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
3
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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
1

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;
}
nsij22
  • 869
  • 10
  • 20
0

One function that will suit your needs and is efficient in its operation would take the following form:

/** strrev - reverse string, swaps src & dest each iteration.
*  Takes valid string and reverses, original is not preserved.
*  If str is valid, returns pointer to str, NULL otherwise.
*/
char *
strrev (char *str)
{
    if (!str) { 
        fprintf (stderr, "%s() Error: invalid string\n", __func__); 
        return NULL; 
    }

    char *begin = str;
    char *end = *begin ? str + strlen (str) - 1 : begin;  /* ensure non-empty */
    char tmp;

    while (end > begin)
    {
        tmp = *end;
        *end-- = *begin;
        *begin++ = tmp;
    }

    return str;
}
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • It's a bad idea for random utility functions to spew their complaints to `stdout`/`stderr` without explicit instruction. – Deduplicator Oct 28 '14 at 21:49
  • I try to be over-inclusive and at least have the function make noise if it receives invalid input. I agree, with your premise regarding error handling, but providing an error handler along with an example function is guaranteed to muddy the water. – David C. Rankin Oct 28 '14 at 21:58
  • 1
    `char *end = str + strlen (str) - 1;` : if `strlen(str)==0` , `end = &str[-1]`.. UB. – BLUEPIXY Oct 28 '14 at 21:59
  • If you consider that invalid input (and decide to check for it), then `abort()` instead of `return 0` after complaining. – Deduplicator Oct 28 '14 at 22:00