Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
Attempt to reverse a string results in a blank string

I'm newbie in C and I'm trying to do some function to reverse a word, here my code:

#include <stdlib.h>
#include <string.h>

// ƒ to revers the word
void lettreCount(char *saisi, int length){
    int i, j = 0;
    char revers[length];

    for (i = length; i  >= 0; --i){  //loop
        *(revers+j) = saisi[i];
        ++j;
    }
    printf("%s\n",revers);
}


int main(){
    char text[30]; //Array for text
    int len;

    printf("Saisissez le text:\n");
    gets(text);

    len = strlen(text);

    lettreCount(text, len);
    
}

but I get all time just an empty string in terminal, how i should to do? thank you

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Strings in C are terminated by a null byte. So when you start your loop with i = length the first character you put in the new array is the null byte. This means you have an empty string.

Start your loop at index length-1 so you start at the last character of the string. Then after the loop, you'll need to manually add the terminating null byte to the destination array.

char revers[length+1];
for (i = length-1; i  >= 0; --i){  //loop
    *(revers+j) = saisi[i];
    ++j;
}
revers[length] = 0;
over 4 years ago · Santiago Trujillo Relatório

0

Do you name all your functions as lettreCount independent on what they are doing?:)

incompatible pointer to integer conversion strchr

If a string s has length characters (the value returned by the function strlen) then the expression s[length] yields the terminating zero character '\0'. The terminating zero character is being written as the first character of the new string in this for loop in its first iteration

for (i = length; i  >= 0; --i){  //loop
    *(revers+j) = saisi[i];
    ++j;
}

As a result the destination array contains an empty string.

The function that creates a reversed string of a given string can be declared and defined the following way

char * copy_reverse( const char *s )
{
    size_t n = strlen( s );
    char *reversed = malloc( n + 1 );

    if ( reversed != NULL )
    {
        reversed[n] = '\0';
        
        for ( char *p = reversed + n; *s; ++s )
        {
            *--p = *s;
        }
    }

    return reversed;
}      
over 4 years ago · Santiago Trujillo Relatório

0

The first thing you do is copy the null terminator of the string into the first position of the array, so it is normal that it prints as an empty string.

void lettreCount(char *saisi, int length){
    int i, j = 0;
    char revers[length];

    for (i = length; i  >= 0; --i){  //loop
        *(revers+j) = saisi[i];

The first character you copy is saisi[length] --> revers[0], so you put a '\0' at the start of your string.

void lettreCount(char *saisi, int length){
    char revers[length];
    int i = length, j = 0;

    while (--i  >= 0) {  //loop
        revers[j++] = saisi[i];
    }
    /* you need to end the string */
    revers[j] = '\0';
    printf("%s\n",revers);
}

Your code corrected is shown below, as you have some other counseils given in the comments:

#include <stdio.h> /* you need also this file to use fgets, gets or printf */
#include <stdlib.h>
#include <string.h>

// ƒ to revers the word
void lettreCount(char *saisi, int length){
    char revers[length];
    int i = length, j = 0;

    while (--i  >= 0) {  //loop
        revers[j++] = saisi[i];
    }
    /* you need to end the string */
    revers[j] = '\0';
    printf("%s\n",revers);
}


int main(){
    char text[100]; //Array for text
    int len;

    printf("Saisissez le text:\n");

    /* Don't use the obsolete gets, use fgets */
    fgets(text, sizeof text, stdin);

    len = strlen(text);

    lettreCount(text, len);

}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda