Weird characters when printing a char-array

I am making a program that takes in a word and reverses it. When the word is small say for e.g: Hello, the output is olleH@$ (note the weird symbols). But when the word is big like "twothousand" the output doesn't have any weird symbols. Why is this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstring>
#include <conio.h>

using namespace std;

int main()
{
    char word[255];
    char rev_word[255];
    int j = 0;
    
    cout << "Enter a word: ";
    cin >> word;
    
    for(int i = strlen(word)-1; i>-1; i--)
    {
        rev_word[j] = word[i];
        j++;
    }
    
    cout << "Reversed: " << rev_word << endl;
    
    getch();
    
    return 0;
}
Last edited on
Why is this?

You got lucky.

Where are you properly terminating your C-string? Remember a C-string must be terminated by a '\0' character.

Thanks dude. What a silly mistake of mine ;o.
Topic archived. No new replies allowed.