zoukankan      html  css  js  c++  java
  • *** strRevert.cpp

    #include <iostream>
    #include <string.h>
    using namespace std;
    // rever letters of a string
    char * strRevert1 (char * src)
    {
        if (src == NULL) return NULL;
        int len = strlen(src);
        for (int i=0; i<len/2; i++)
        {
            src[i] = src[i] ^ src[len-1-i];
            src[len-1-i] = src[i] ^ src[len-1-i];
            src[i] = src[i] ^ src[len-1-i];
        }
        return src;
    }
    // revert words of a string - this solution is not good
    char * strRevert2 (char * src)
    {
        if (src == NULL) return NULL;
        int len = strlen(src);
        char * orgSrc = src;
        char des[len+1];
        des[len] = '';
        char * pDes = &des[len-1];
        char * pWord = orgSrc;
        int spaceCount = 0;
        if (*orgSrc == ' ') spaceCount = 1;
        while(1)
        {
            // end of a word or a ' ' set, copy the word or ' ' set into des[]
            if (((*orgSrc==' ')&&(!spaceCount)) || ((*orgSrc!=' ')&&(spaceCount)) || (*orgSrc==''))
            {
                char * index = NULL;
                index = orgSrc - 1;
                do
                {
                    *pDes = *index;
                    pDes--;
                    index--;
                }
                while (index >= pWord);
                if (*orgSrc=='') break;
                // prepare for a new word or new ' ' set
                pWord = orgSrc;
                spaceCount = (*orgSrc==' ')?(spaceCount+1):(0);
            }
            // continue with ' ' set
            else if ((*orgSrc==' ') && (spaceCount))
            {
                spaceCount++;
            }
            // continue with a word
            orgSrc++;
        }
        strcpy (src, des);
        return src;
    }
    // revert words of a string - this solution is better than strRevert2
    char * strRevert3 (char * src)
    {
        char * start=src, *end=src, *ptr=src;
        while (*ptr++ != '');
        end = ptr -2;
        while(start<end)
        {
            swap(*start++, *end--);
        }
        start = src;
        end = ptr -2;
        ptr = start;
        while (*ptr++ !='')
        {
            if (*ptr==' ' || *ptr=='')
            {
                end = ptr-1;
                while (start<end)
                swap(*start++, *end--);
                start = end = ptr+1;
            }
        }
    return src;
    }
    int main()
    {
        char a[19] = "who are you ?";
    cout << "revert: " << strRevert3(a) << endl;
        cout << "revert: " << strRevert2(a) << endl;
        cout << "revert: " << strRevert1(a) << endl;
        return 0;
    }
  • 相关阅读:
    VS2008编写MFC程序--使用opencv2.4()
    November 02nd, 2017 Week 44th Thursday
    November 01st, 2017 Week 44th Wednesday
    October 31st, 2017 Week 44th Tuesday
    October 30th, 2017 Week 44th Monday
    October 29th, 2017 Week 44th Sunday
    October 28th, 2017 Week 43rd Saturday
    October 27th, 2017 Week 43rd Friday
    October 26th, 2017 Week 43rd Thursday
    October 25th, 2017 Week 43rd Wednesday
  • 原文地址:https://www.cnblogs.com/superrunner/p/10165248.html
Copyright © 2011-2022 走看看