0

The code is the following. It compiles without warnings or errors yet nothing is printed out on the terminal. Any ideas why?

I think the answer must be obvious yet, I cant see it.

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

char* palindrome (char *word);

main()
{
    char *leksi, *inverse_leksi;
    //leksi means word
    leksi = malloc(sizeof(char)*256);
    inverse_leksi = malloc(sizeof(char)*256);
    
    gets(leksi);
    
    inverse_leksi = palindrome(leksi);
    puts(inverse_leksi);
}

char* palindrome (char *word)
{
    int i;
    char *inverse_word;
    
    inverse_word = malloc(sizeof(char)*256);
    
    for (i = 0; i < strlen(word) + 1; i++)
    {
        inverse_word[i] = word[strlen(word) - i];
    }
    
    return inverse_word;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Kos
  • 83
  • 8
  • 1
    Don't use gets , please read this : https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – MED LDN Jan 30 '21 at 16:00
  • It's not true that nothing is printed out. A blank line is printed out, meaning that palindrome returns a zero-length string, or in other words a character array whose first byte is 0. So what does `inverse_word[i] = word[strlen(word) - i];` do if `i` is 0? If you don't know how to answer, use a debugger. – rici Jan 30 '21 at 16:10
  • Also, computing `strlen` every time you execute that loop is painfully inefficient. Do some research into how `strlen` works. The returned value won't change. – rici Jan 30 '21 at 16:12
  • @rici I changed it to `inverse_word[i] = word[strlen(word) -1 - i];` and it worked. I'm new at C so i havent really done any research. – Kos Jan 30 '21 at 16:15
  • Do not use `gets` - it is no longer supported as of the C11 standard (it's been a known malware exploit since the late 1980's). Use [`fgets`](https://pubs.opengroup.org/onlinepubs/009696699/functions/fgets.html) instead, but be aware that it will store the newline if there's room in the target buffer, so you'll have to account for that. – John Bode Jan 30 '21 at 16:23
  • 1
    @kos: when you're new, you need to do more research :-) – rici Jan 30 '21 at 16:58

2 Answers2

1

The characters in a valid c-style string are in the indexes 0 to strlen(var) - 1. The character in the index strlen(var) is the null-terminator (\0). In the loop you're assigning this character to invers_word[0], so you're returning an empty string. Instead, you should iterate one character less, and then explicitly handle the null-terminator string. Additionally, you have an off-by-one error in the index calculation:

// iterate up to the length of the string, without the \0
for (i = 0; i < strlen(word); i++)
{
    inverse_word[i] = word[strlen(word) - i - 1];
}
inverse_word[strlen(word)] = '\0'; // explicitly handle \0
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Use this: inverse_word[i] = word[strlen(word) - i - 1];. If you don't have the -1, the first character to get copied will be \0.

Don't use gets. There are better alternatives.

babon
  • 3,615
  • 2
  • 20
  • 20