0

I am trying to create a duplicate of a string but reversed. I am able to strcpy each char and print them individually but I get nothing when I print the entire duplicate string.

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

int     main(void)
{
    char    str[101] = "Lieur a Rueil";
    char    temp[101];
    int     i;
    int     j;

    i = 0;
    j = strlen(str);

    while (str[j] - 1)
    {
        strcpy(&temp[i], &str[j]);
        printf("%c", temp[i]);
        j--;
        i++;
    }
    printf("\n");
    printf("temp: %s\n", temp);
    return (0);
}

output:

lieuR a rueiL
temp:
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Balou
  • 21
  • 7
  • 4
    Why are you using `strcpy` instead of copying a single character? – Eugene Sh. Sep 27 '21 at 20:43
  • 3
    What do you think `str[j] - 1` actually does? Maybe you meant `str[j - 1]` instead? Though `i < j` wouuld make more sense. And `strcpy()` is not how you swap characters of a string. In any case, why not just use an existing function like `strrev()` or equivalent instead? – Remy Lebeau Sep 27 '21 at 20:44
  • 1
    No, you *cannot* `strcpy()` individual characters. The `strcpy()` function does not do that. But you can use ordinary assignment: `temp[i] = str[j]`. Don't forget to add a string terminator at the end of the copy. – John Bollinger Sep 27 '21 at 20:45
  • Indeed, strcpy is a mistake, my bad ^^ – Balou Sep 27 '21 at 20:55
  • (a) terminate the new string with '\0', (b) use simple `temp[x] = str[y]` to copy chars – kofemann Sep 27 '21 at 21:02

3 Answers3

1
#include <stdio.h>
#include <string.h>
#include <stdint.h>
 
int main(void)
{
    char    str[101] = "Lieur a Rueil";
    char    temp[101];
    size_t  sz = strlen(str);
 
    for(size_t i=0;i<sz; ++i)
    {
        temp[sz-i-1] = str[i];
    }
    temp[sz]=0;
 
    printf("temp: %s\n", temp);
    return (0);
}

Output:

temp: lieuR a rueiL
abelenky
  • 63,815
  • 23
  • 109
  • 159
1

With minimal changes to original code:

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

int     main(void)
{
    char    str[101] = "Lieur a Rueil";
    char    temp[101];
    int     i;
    int     j;

    i = 0;
    j = strlen(str);

    temp[j] = '\0';
    while (j)
    {        
       temp[j -1] = str[i];
        printf("%c", temp[j]);
        j--;
        i++;
     }
     printf("\n");
     printf("temp: %s\n", temp);
     return (0);
}
kofemann
  • 4,217
  • 1
  • 34
  • 39
0

This while loop

while (str[j] - 1)
{
    strcpy(&temp[i], &str[j]);
    printf("%c", temp[i]);
    j--;
    i++;
}

does not make a sense. The condition of the loop can invoke undefined behavior due to accessing memory beyond the array. The same problem exists with the call of strcpy.

The main logic of the program can be implemented simpler. For example

size_t i = 0;

for ( size_t j = strlen( str ); j-- != 0; i++ )
{
    temp[i] = str[j];
}

temp[i] = '\0';

printf("\ntemp: %s\n", temp);

Here is a demonstrative program.

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

int main(void) 
{
    char str[] = "Lieur a Rueil";
    char temp[sizeof( str )];
    
    size_t i = 0;

    for ( size_t j = strlen( str ); j-- != 0; i++ )
    {
        temp[i] = str[j];
    }

    temp[i] = '\0';

    printf("\ntemp: %s\n", temp);   
}   

The program output is

temp: lieuR a rueiL
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335