Invert string in C

Asked

Viewed 14,489 times

6

I’m trying to reverse a string in C but the function is clearing it:

for(i = 0; i < (int)(size/2); i++)
{
   tmp = buffer[i]; //armazena o character inicial
   buffer[i] = buffer[size - i]; //Troca o character da ponta oposta
   buffer[size - i] = tmp; //Armazena o character inicial no buffer
}

'Cause I wipe the string when I make strlen again from that string?

The Hello string has 5 characters, so the size variable will be 5.~ by changing the values in the code above:

  • tmp = buffer[0] ('H character')
  • buffer[i] = buffer[size - i] (the position where the character H was will now have the value of the character of the position [5 - 0], that is, position 5, which corresponds to the character 'o')
  • buffer[size - i] = tmp (since tmp had the character value already stored in buffer[i], then the position of the'o' character will have the character value 'H')

Is this analysis correct? What is the problem?

  • 1

    Done. Thanks again

4 answers

11


The problem is that "size - i" will pick up a character beyond the length of the string. For example, a string with size 5 (size=5) will have characters in s[0], s[1], s[2], s[3] and s[4], but s[5] will be character 0. Once this character is played in s[0], the string will appear to have zero length from then on, despite continuing to occupy six memory positions.

In my view the program should be fixed to buffer[size - i - 1]. I think (int) in size/2 is also unnecessary if size is integer, because in C an integer division always returns an integer rounded down.

  • I do not believe, I forgot that the array starts at 0... I thought to start at 0 and counted to start at 1.

5

The biggest problem is that you are forgetting to withdraw 1 in the final position.

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

int main(void) {
    char buffer[] = "Hello";
    size_t size = strlen(buffer);
    for(int i = 0; i < size / 2; i++) {
       char tmp = buffer[i]; //armazena o character inicial
       buffer[i] = buffer[size - i - 1]; //Troca o character da ponta oposta
       buffer[size - i - 1] = tmp; //Armazena o character inicial no buffer
    }
    printf("%s", buffer);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Yes, as I said, I thought of the array starting at 0 and counted the characters starting at 1. My stupidity. Thank you

0

Following (tested) solution to the proposed problem:

/* reverse.c */

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

char * reverse( char * s )
{
    int length = strlen(s) ;
    int c, i, j;

    for (i = 0, j = length - 1; i < j; i++, j--)
    {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }

    return s;
}


int main( void )
{
    char str[] = "Ola Mundo!";

    printf("%s\n", reverse(str) );

    return 0;
}

/* fim-de-arquivo */

0

bro would be better like this. Ignore this 255.

   char nome[255];
   int tam;
   scanf("%254[^\n]", nome);
   tam=strlen(nome);
    for(i=tam-1;i>=0;i--){

 printf("%c",nome[i]);

    }


Browser other questions tagged

You are not signed in. Login or sign up in order to post.