且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从在C字符串中删除标点

更新时间:2023-12-03 19:16:40

所提供的只是一个使用的算法的草图功能ctype.h$c$c>:

Just a sketch of an algorithm using functions provided by ctype.h:

#include <ctype.h>

void remove_punct_and_make_lower_case(char *p)
{
    char *src = p, *dst = p;

    while (*src)
    {
       if (ispunct((unsigned char)*src))
       {
          /* Skip this character */
          src++;
       }
       else if (isupper((unsigned char)*src))
       {
          /* Make it lowercase */
          *dst++ = tolower((unsigned char)*src);
          src++;
       }
       else if (src == dst)
       {
          /* Increment both pointers without copying */
          src++;
          dst++;
       }
       else
       {
          /* Copy character */
          *dst++ = *src++;
       }
    }

    *dst = 0;
}

标准地告诫:没有经过充分测试;精炼和优化留下作为运动至读卡器。

Standard caveats apply: Completely untested; refinements and optimizations left as exercise to the reader.