How do I trim leading/trailing whitespace in a standard way?

后端 未结 30 2378
一个人的身影
一个人的身影 2020-11-22 02:06

Is there a clean, preferably standard method of trimming leading and trailing whitespace from a string in C? I\'d roll my own, but I would think this is a common problem wit

30条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 02:38

    Another one, with one line doing the real job:

    #include 
    
    int main()
    {
       const char *target = "   haha   ";
       char buf[256];
       sscanf(target, "%s", buf); // Trimming on both sides occurs here
       printf("<%s>\n", buf);
    }
    

提交回复
热议问题