一阵狂风 发表于 2017-6-1 12:56:40

[算法练习]字符.字符串删除,字符提前

#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;
}

//用数组下标标志sub字符串,同hash原理
char* Del_Speciol_Str(char *str, char ch[], int n)
{
        if( str == NULL || *str == '\0' || n == 0 || ch == '\0' )
                return NULL;

        char temp = {0};

        for (int i = 0; i < n; ++i)
        {
                temp] = 1;
        }


        int dst = 0;
        int src = 0;

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

        return str;
}


int Del_Speciol_Str_Ex(char *str, char *substr)
{
        if(str == NULL || *str == '\0' || substr == NULL || substr == '\0' )
                return 0;

        char *pSrc = NULL;
        char *pDet = NULL;
        char *p,*pSub;

        pSrc = pDet = str;

        int count = 0;
        //O(n) + O(n)
        while(*pSrc)
        {
                p = pSrc;
                pSub = substr;

                while(*p && *p == *pSub)
                {
                        p++;
                        pSub++;
                }

                if(*pSub == '\0')//==0就说明匹配成功了 成功了就将当前的指针付给pSrc指针继续找后面还有没有匹配的字符串
                {
                        pSrc = p;
                        count++;
                }else
                {
                        *pDet++ = *pSrc++;
                }
        }
        *pDet = 0;

        return count;
}

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("del_char:\n%s\n",str6);
        Del_speciol_ch_Ex(str6,'*');

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

        char strTest = "MallocFree is luanmaMr.Zhou";
        char ch = "luanma";
        printf("del_chars:\n%s\n", strTest);
        //Del_Speciol_Str(strTest,ch,5);
        int subnum = Del_Speciol_Str_Ex(strTest, ch);
        printf("%s\nSubNum:%d\n",strTest ,subnum);

        getchar();
}
页: [1]
查看完整版本: [算法练习]字符.字符串删除,字符提前