How to put character from variable into array in C? -



How to put character from variable into array in C? -

i want set character variable character array in c. want print reversed array afterwards can see that's not issue now.

this code i've got far:

as stdin i'm using txt-file "< input.txt" command line argument, , has 57 characters.

#include <stdio.h> #include <stdlib.h> int main() { int counter = 0; char character_array[57]; int = 0; int j = 0; char character = 0; // while eof not encountered read each character while (counter != eof) { // print each character printf("%c", counter); // go on getting characters stdin/input file counter = getchar(stdin); // set each character array character_array[j] = { counter }; j = j + 1; } // print array elements in reverse order (i = 58; > 0; i--) { character = character_array[i]; printf("%c", character); } homecoming 0; }

my ide says @ line 35 after first curly brace "expected expression".

// set each character array character_array[j] = { counter };

so guess fails there. assume cannot set character variable in array? how go doing otherwise?

ps: i'm new c.

remove { , } in line looks :

character_array[j] = counter ;

improved code:

#include <stdio.h> #include <stdlib.h> int main() { int counter = 0; char character_array[57]; int = 0; int j = 0; char character = 0; // while eof not encountered read each character while ((counter = getchar()) != eof) { // print each character printf("%c", counter); character_array[j] = counter; j++; } (i = j-1; >= 0; i--) { printf("%c", character_array[i]); } }

c arrays string character

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -