> for (i=0; i < strlen(string); i++)
You're lucky this works at all.
string starts off uninitialised, so who knows what strlen() on it will return.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
> 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
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
> better way...
Still two problems
- what happens at EOF
- the loop can exit with i == SIZE, which means your \0 is out of bounds.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
Salem is right...
String is uninitialised... you get junk for len... for sure...
Then why bring it up. He already had others confirm this. :icon_rolleyes:Yes, EOF is an issue... but while entering the character where does EOF comes into picture?...
Anytime you pipe in a file or press the EOF keystroke.
This thread is a great examply why people need to stopgiving working answers to questions. Lots of code that teaches bad techniques. Just help the original poster find his own answer with hints and suggestions, not poorly designed code.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
> I put that in just to show that one gets zero or nonsense,
Or the right answer, or a segmentation fault.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953