2

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

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Note: [don't use `gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). – Chris Jan 08 '22 at 16:26

3 Answers3

3

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;
kirjosieppo
  • 617
  • 3
  • 16
dbush
  • 205,898
  • 23
  • 218
  • 273
2

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;
}      
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
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);

}
Luis Colorado
  • 10,974
  • 1
  • 16
  • 31