zoukankan      html  css  js  c++  java
  • *** 自写代码:反转字符串,但指定子串不反转 (使用了stack)

    #include <iostream>
    #include <string.h>
    #include <stack>
    using namespace std;
    char * revStrExcludeSub (char * src, char * sub)
    {
        if ((src==NULL) || (sub==NULL)) return NULL;
        char * head=src, * tail=src, * pSub=sub;
        stack<char> st;
        while (*head != '')
        {
            while ((*head==*pSub) && (*pSub!=''))
            {
                ++head;
                ++pSub;                
            }
            // find one match with sub string
            if (*pSub == '')
            {
                char * p = head-1;
                while (p >= tail)
                {
                    // cout << "1 push: " << *p <<endl;    
                    st.push(*p--);
                }
                tail = head;
                pSub = sub;
            }
            else
            {
                st.push(*tail);
                // cout << "2 push: " << *tail <<endl;
                tail++;
                head = tail;
                pSub = sub;
            }
        }
        head = src;
        while (!st.empty())
        {
            *head++ = st.top();
            st.pop();
        }
        *head = '';
        return src;
    }
    int main()
    {
        char a[] = "123434578345678";
        char b[] = "345";
        cout << "input:  " << a << " - sub string: " << b << endl;
        cout << "output: " << revStrExcludeSub (a, b) << endl;
        return 0;
    }
  • 相关阅读:
    RegExp
    svn操作
    前端跨域请求
    UML
    excel 常用设置
    python中 cmp
    python global nonlocal
    python常见异常提示
    table边框和td的width失效
    display_css
  • 原文地址:https://www.cnblogs.com/superrunner/p/10165210.html
Copyright © 2011-2022 走看看