3

Yesterday I had to solve an exam exercise, which, unfortunately, I failed.. The exercise was to create a function in C with the following rules:

  • Write a function that takes a string and displays the string in reverse order followed by the newline.
  • Its prototype is constructed like this : char *ft_rev_print (char *str)
  • It must return its argument
  • Only allowed to use function 'write'(so no printf or others)

With that information I wrote :

int     ft_strlen(char *str) /*to count the length of the original string*/
{
    int     i;
    i = 0;
    while (str[i])
            i++;
    return (i);
}
char    *ft_rev_print (char *str)
{
    int     i;

    i = ft_strlen(str);
    while (i)
    {
            write (1, (str +1), 1);
            i--;
    }
    return (str);             /*returning its argument */
}

int     main(void)     /*IT HAD TO WORK WITH THIS MAIN, DID NOT WROTE THIS MYSELF!*/
{
    ft_rev_print("rainbow dash");
    write(1, "\n", 1);
    return (0);
}

I tried for ages to get it to work, but failed.. So now I'm breaking my head over this. What did i do wrong ? What did i miss?

Thanks in advance !

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
πrre
  • 33
  • 5

3 Answers3

3

Your while loop is wrong, you start with i=0 and iterate while it's not zero, so no iterations will be done.

What you should do is:

  • initialize i so that it is the index of the last character
  • loop as long as it's a valid index
  • print the i-th character, not always the second (at index one)
char *ft_rev_print (char *str)
{
    int i;

    i = ft_strlen(str) - 1; // <-- Initialize i to be the index of the last character
    while (i >= 0) // <-- Loop as long as it's valid
    {
        write (1, (str+i), 1); // <-- Print the i-th character
        i--;
    }
    return (str);
}
Dario Petrillo
  • 980
  • 7
  • 15
2

For starters your teachers are not enough qualified. The functiuon should be declared at least like

char * ft_rev_print( const char *str );
                     ^^^^^

because the passed string is not changed within the function.

You forgot to call the function ft_strlen.

It seems you mean

i = ft_strlen( str );

As a result this loop

i = 0;
while (i)
{
    //...
}

is never executed because initially i is equal to 0 and the condition of the loop at once evaluates to false.

Also in this call

write (1, (str +1), 1);
          ^^^^^^^^

you are always trying to output the second symbol of the string.

Also the output of the new line character '\n' should be within the function according to its description.

The function can look the following way as it is shown in the demonstrative program below (instead of the non-standard function write I will use the function putchar but you can substitute it for write yourself)

#include <stdio.h>

char * ft_rev_print( const char *str )
{
    const char *p = str;
    
    while ( *p ) ++p;
    
    while ( p != str ) putchar( *--p );  // substitute the call of putchar for write( 1, *--p, 1 )
    
    putchar( '\n' );  // substitute the call of putchar for write( 1, "\n", 1 )
    
    return ( char * )str;
}

int main(void) 
{
    ft_rev_print( "rainbow dash" );
    
    return 0;
}

The program output is

hsad wobniar
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Awesome explanation ! thanks! To be fair they actually said the string cannot be modifiable, but i forgot that one. so i needed to compare the str with a constant char it would seem. Really helpful ! – πrre Mar 27 '21 at 09:01
1

Hey I have tried your question and there is a small point I would like to add,In your question you have written the length of your string and also the part below in your code:

  write (1, (str +1), 1);

was not correct so I corrected it and in that basically we are adding the back of previous characters like this and error was in a while loop condition:

write (1,(str+i),1);

You can full prototype here:

char    *ft_rev_print (char *str)
{
    int     i;
     i = ft_strlen(str);//returning the length of string
     while (i>=0)
     {
            write (1,(str+i),1);
            i--;
     }
    return (str);             /*returning its argument */
}
Dharman
  • 30,962
  • 25
  • 85
  • 135