梦的忧伤 发表于 2017-6-1 13:33:07

[算法练习]将字符串中*提前&&删除指定字符

//思路:只要不是星号的都交给写指针写入,忽略*
//问题可以转换为:删除字符串中的特定字符 只不过这个是从尾巴开始
#include <stdio.h>

size_t _strlen(char *str)
{
        return (NULL == str || *str =='\0') ? 0 : _strlen(str+1) + 1;
}

//思路:只要不是星号的都交给写指针写入,忽略*
//问题可以转换为:删除字符串中的特定字符 只不过这个是从尾巴开始
char* str_TiQian(char *str)
{
        if(NULL == str || *str == '\0')
                return NULL;

        int len = 0;
        int Dst = 0;
        int count = 0;

        len = Dst = _strlen(str) - 1;

        do
        {
                if(str != '*')
                        str = str;//这里是先执行str = str 然后才dst-1;
                else
                        count ++;

        } while (len--);

        for (int i = 0; i < count; i++)
        {
                str = '*';
        }

        printf("%s\n",str);

        return str;
}

//思路同上
char* str_TiQian_Ex(char* str)
{
        if(NULL == str || '\0' == *str)
                return NULL;

        char *pRead = 0;
        char *pWrite = 0;

        pRead = pWrite = str + _strlen(str) - 1;

        do
        {
                if(*pRead != '*')
                        *pWrite-- = *pRead;

        } while (pRead-- != str);//先比较再赋值

        while (pRead <= pWrite)
        {
                *pRead++ = '*';//先执行后面的在执行加操作
                //相当于
                //*pRead = '0';
                //pRead ++;
        }


        printf("%s\n",str);

        return str;
}


char* Del_speciol_ch(char *str, char ch)
{
        if(NULL == str || *str == '\0')
                return NULL;

        char *pRead = 0;
        char *pWrite = 0;

        pRead = pWrite = str;

        do
        {
                if(*pRead != ch)
                        *pWrite++ = *pRead;
        } while (*pRead++ != '\0');

        printf("%s\n",str);
       
        return str;
}

char* Del_speciol_ch_Ex(char *str, char ch)
{
        if(NULL == str || *str == '\0')
                return NULL;

        int sour = 0;
        int dst = 0;

        do
        {
                if(str != ch)
                        str = str;
        } while (str != '\0');

        printf("%s\n",str);

        return str;
}

void main()
{
       
        char str0 = "hel*lo w*or*ld";
        printf("%s\n",str0);
        str_TiQian(str0);

        char str1 = "***hel*l**o w*o**r*ld**";
        printf("\n%s\n",str1);
        str_TiQian(str1);

        char str3 = "***hel*l**o w*o**r*ld**";
        printf("\n%s\n",str3);
        str_TiQian_Ex(str3);

        char str4 = "hel***lo w*or*ld";
        printf("\n%s\n",str4);
        str_TiQian_Ex(str4);

        printf("\n----------\nDel * \n");
        char str5 = "hel***lo w*or*ld";
        printf("\n%s\n",str5);
        Del_speciol_ch(str5,'*');
        Del_speciol_ch(str3,'*');

        char str6 = "hel***lo w*or*ld";
        char str7 = "***hel*l**o w*o**r*ld**";
        printf("\n%s\n",str6);
        Del_speciol_ch_Ex(str6,'*');

        printf("\n%s\n",str7);
        Del_speciol_ch_Ex(str7,'*');

        getchar();
}
页: [1]
查看完整版本: [算法练习]将字符串中*提前&&删除指定字符