Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing an array of chars

Let's say I have "game.abc" as a string (array of chars) and I want to get the chars that are before the dot. What you would deem to be the most efficient way to do this in C?

Is a "while until . copy char to char" the way to go or is there any other way?

I hope this question can help others also.

Thanks!

like image 907
Qosmo Avatar asked Nov 29 '25 08:11

Qosmo


2 Answers

you can also use strchr() followed by strncpy() to find first instance of '.' in your string and then copy all characters before it to another string

like image 167
binW Avatar answered Nov 30 '25 23:11

binW


It depends on what you're trying to optimize for. If for readability an ease of understanding/maintaining, then binW's suggestion is correct.

If you're chasing cycles and only want to visit each character of the input once, then it's better to do the two operations (searching for the dot and copying) in parallel as you suggest by doing them both in the same loop.

Note that the latter smells very much of premature optimization though.

like image 33
unwind Avatar answered Dec 01 '25 00:12

unwind