如何从在C字符串中删除标点 [英] How to remove punctuation from a String in C

查看:253
本文介绍了如何从在C字符串中删除标点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找从字符串中删除所有的标点,让所有大写字母在C以下的情况下,有什么建​​议?

I'm looking to remove all punctuation from a string and make all uppercase letters lower case in C, any suggestions?

推荐答案

所提供的只是一个使用的算法的草图功能<一个href=\"https://www.opengroup.org/onlinepubs/000095399/basedefs/ctype.h.html\"><$c$c>ctype.h:

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.

这篇关于如何从在C字符串中删除标点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆