string[i]=getchar(); I don't think this will work properly. getchar() expects you to hit enter after the input of each character which means that '\n' will be left in the input buffer which will be read the next time getchar() executes. Which means that each character will alternate with a '\n'.
I might be wrong though. Feel free to correct me.
string[i]=getchar(); I might be wrong though. Feel free to correct me.
You're wrong. getchar() does not behave in quite the way you described. Consider yourself corrected.
Salem is right. Once you get past the concern he pointed out, there are also additional problems that the code has no error checking, nor does it terminate the input string with a zero byte.
>You're wrong. getchar() does not behave in quite the way you described.
My bad. Did some research on getchar(). Seems getchar() doesn't need enter to be pressed after the input of each character. It can only see the input buffer after hitting the enter. Gotta get my basics straightened.
> printf("len = %d\n", strlen(string)); // len = 0
Nope, read my first post again.
string isn't initialised, so your length is junk.
If you get zero, that just makes you lucky.
> printf("len = %d\n", strlen(string)); // len = 0
Nope, read my first post again.
string isn't initialised, so your length is junk.
If you get zero, that just makes you lucky.
The rest is getting better though.
Salem is right...
String is uninitialised... you get junk for len... for sure...
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.