C 语言实例 – 删除字符串中的特殊字符

C语言教程评论

C 语言实例 - 删除字符串中的特殊字符

删除字符串中的除字母外的字符。

实例

#include<stdio.h>

int main()
{
    char line[150];
    int i, j;
    printf("输入一个字符串: ");
    fgets(line, (sizeof line / sizeof line[0]), stdin);

    for(i = 0; line[i] != '\0'; ++i)
    {
        while (!( (line[i] >= 'a' && line[i] <= 'z') 
                 || (line[i] >= 'A' && line[i] <= 'Z') || line[i] == '\0') )
        {
            for(j = i; line[j] != '\0'; ++j)
            {
                line[j] = line[j+1];
            }
            line[j] = '\0';
        }
    }
    printf("输出: ");
    puts(line);
    return 0;
}

输出结果为:文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-shanchuzifuchuanzhongdeteshuzifu.html

输入一个字符串: now4#$1coder
输出: nowcoder
文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-shanchuzifuchuanzhongdeteshuzifu.html文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-shanchuzifuchuanzhongdeteshuzifu.html
运营不易,
感谢支持!
weinxin
我的微信
我的微信公众号
我的微信公众号扫一扫
weinxin
我的公众号
 
公式库网
  • 本文由 公式库网 发表于 2021年12月7日20:45:04
  • 转载请务必保留本文链接:https://www.gongshiku.com/html/202112/c-yuyanshili-shanchuzifuchuanzhongdeteshuzifu.html

发表评论