I want to put AAA,BBB,123,..., inside summary. How can I do so?
In other words, I want to read sentence until the LAST occurence of , (comma), and then grab everything before it (including the comma), in order to put it inside summary
Ya but, "ZZZ" may be just an example and also a variable, not a constant.
The catch is that the simple search (for commas) requires backtracking or look ahead.
Maybe:
while traversing the entire string from left to right
{
copy a character to the destination buffer
if it is a comma
{
store its index
}
}
increment the index
store a null to terminate the destination string
optionally maybe use strtok() to speed the same algorithm up by searching for commas faster if it's understood to be optimized code.
I am assuming here; the resulting string having a penultimate character "comma" with a terminating null is not desired, rather, the null should replace the "comma" then truncate the remaining characters.
"aaa,bbb,..." is just an example, the actual data varies. @anon57585045 is correct.
The relevant information here is that the whole char array contains datas that are comma seperated.
As suggested by @anon57585045 again, my current implementation of this already consists in using strtok(). I don't quite like it because I later need to stitch all the parts together (AAA and , and BBB and , and 123 and , and ... and , and etc) to finally obtain the expected data that is AAA,BBB,123,..., , which is a very laborious way of doing it.
As suggested by @dougp, I came across strrchr() which seems to be relevant to my need, but I was not able to make it work.
You were right I was missing the null terminating char.
I had to add +1 to the char position so that I have the comma included.
This seems to work as expected.
Is it acceptable or should I improve something?
Just saw your edits.
I do not want to alter the original string indeed.
So I will add a strlen test condition and exit if too long.
Thank you very much for your time @J-M-L