traversing C string: get the last word of a string

前端 未结 5 1090
终归单人心
终归单人心 2021-01-13 03:26

how would you get the last word of a string, starting from the \'\\0\' newline character to the rightmost space? For example, I could have something like this where str coul

5条回答
  •  攒了一身酷
    2021-01-13 03:38

    The best way to do this is to take advantage of existing solutions. One such solution (to a much more general problem) is Perl Compatible Regular Expressions, an open-source regular expression library for C. So, you can match the string "my cat is yellow" with the regular expression \b(\w+)$ (expressed in C as "\b(\w+)$") and keep the first captured group, which is "yellow."

提交回复
热议问题