Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why i can't use strrev() in my program

+10 votes
asked Dec 1, 2021 by Achraf (220 points)
everytime i try to use strrev() in my program i recieve a warning !

this is my program :

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

int main(){
    char mot[50];
    char mot_reverse[50];
    printf("Entrer le mot a inverser : ");
    int i;
    for(i=0; i<strlen(mot); i++)
            mot[i] = toupper(mot[i]);
    return 0;

1 Answer

0 votes
answered Dec 2, 2021 by Peter Minarik (88,520 points)
edited Dec 2, 2021 by Peter Minarik

If you look at the warnings and errors, you'll see the following:

Compilation failed due to following error(s).

main.c:14:25: warning: implicit declaration of function ‘strrev’; did you mean ‘strsep’? [-Wimplicit-function-declaration]
   14 |     strcpy(mot_reverse, strrev(mot));
      |                         ^~~~~~
      |                         strsep

/usr/bin/ld: /tmp/cc9hnaOp.o: in function `main':
main.c:(.text+0xb6): undefined reference to `strrev'
collect2: error: ld returned 1 exit status

What the compiler is telling you here is that strrev() is not found. The linker can not link the program ("undefined reference"). The problem here is that this is a non-standard C function. If you scroll to the notes section, you can see it there:

Note: This is a non-standard function that works only with older versions of Microsoft C.

The standard C string library does not define strrev().

This is not the end of the world. You can just write your own strrev() implementation. :)

#include <string.h>

char * strrev(char * str)
    if (!str || !*str) // The reverse of a NULL string or an empty string is itself.
        return str;

    // Starting two indices: left and right, keep moving the left toward the right (increment) and the right toward the left (decrement) as long as left is less than right
    for (int left = 0, right = strlen(str) - 1; left < right; left++, right--)
        // Swap the characters at the left and right positions
        char tempChar = str[left];
        str[left] = str[right];
        str[right] = tempChar;
    return str;
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.