zoukankan      html  css  js  c++  java
  • [转载]c删除字符串中指定字符

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    int del_char(char* src, char c)
    {
            if ( NULL == src)
            {
                    fprintf(stderr, "del_char failed: invalid input./n");
                    return -1;
            }
            char* pchTemp = (char*)malloc(strlen(src) + 1);
            if ( NULL == pchTemp)
            {
                    fprintf(stderr, "del_char failed: malloc failed/n");
                    return -1;
            }
            int num = 0;
            int i = 0;
            int j = 0;
            while ( src[i] != '/0')
            {
                    if ( src[i] != c)
                    {
                pchTemp[j++] = src[i++];
                    }
                    else
                    {
                            i++;
                            num++;
                    }
            }
            pchTemp[j] = '/0';
            if ( 0 != num)
            {
                    strncpy(src, pchTemp, strlen(pchTemp) + 1);
            }
            free(pchTemp);
            return num;
    }
    int main(int argc, char* argv[])
    {
            if ( 3 != argc)
            {
                    fprintf(stderr, "Usage: %s [src] [c]/n", argv[0]);
                    return -1;
            }
            char szSrc[512] = {0};
            snprintf(szSrc, sizeof(szSrc), "%s", argv[1]);
            char c = argv[2][0];
            int num = 0;
            if ( (num = del_char(szSrc, c)) < 0)
            {
                    return -1;
            }
            else
            {
                    printf("num: %d after del %c: %s/n", num, c, szSrc);
                    return 0;
            }
            return 0;
    }
  • 相关阅读:
    Lua中table的实现-《Lua设计与实现》
    unity3D 知识点随手记
    游戏随笔之事件系统的设计
    游戏随笔之游戏资源池的设计
    MMORPG战斗系统随笔(四)、优化客户端游戏性能
    MMORPG战斗系统随笔(三)、AI系统简介
    vue-cli3关闭eslint语法检查
    Vue3+与Vue2共存
    php7中??和?:的区别
    react-hook生命周期
  • 原文地址:https://www.cnblogs.com/fx2008/p/2190503.html
Copyright © 2011-2022 走看看