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

int main()
{
    char s[100],temp1[100];
    int i;

    printf("Some word ");
    scanf("%s", s);

    for (i = 0; s[i] != '\0'; ++i);


    int a = i-1;
    for (int k = 0; k < i; k++) {

        temp1[k] = s[a];
        a--;
    }

    printf("%s", temp1);


    system("pause");
}

I am trying to reverse a given word but I just get some other things with my answer. I know I just can search for it but I wanna learn why I failed like that so please help me.

süleyman
  • 93
  • 10
  • 1
    Replacing `temp1[100]` with `temp1[100] = {0}` will fix the problem. – Sergey Kalinichenko Dec 06 '16 at 16:43
  • This question was obvious but please make an effort to describe the actual problems in future. 'Like that" is not a good description; for a good description, please see guidelines here: [mcve]. – anatolyg Dec 06 '16 at 16:45
  • if it's allowed, you can replace `for (i=0; s[i]!='\0'; ++i);` with `#include ` .. `i=strlen(s)` or `int a=strlen(s)-1);` – yano Dec 06 '16 at 16:48

1 Answers1

3

You need to null terminate the result. Add

temp1[i] = '\0';

Also, change scanf() to

scanf("99%s", s);
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97