Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to efficiently parse a string in c

Tags:

c

string

char

I have:

char *str = "abc def  abcdef ghi   xyz";

I want to assign

const char *a = "abc"; 
const char *b = "def";
const char *c = "abcdef";
const char *d = "ghi";
const char *e = "xyz";

Key here is that number of spaces can be more than one.

Please suggest an efficient way.

like image 572
hari Avatar asked Jan 25 '26 07:01

hari


1 Answers

Efficency is in the eye of the beholder. But take a look at strtok; however, you will need to work with a copy of the string that can be modified.

Note that char *str = "blah" is not a good idea. You should be using const char *str = "blah".

like image 184
Oliver Charlesworth Avatar answered Jan 26 '26 23:01

Oliver Charlesworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!