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;
    }
  • 相关阅读:
    MS SQL Server 定时任务实现自动备份
    Python日期的加减等操作
    C# DbHelperSQL 类,从东软生成器提取而来
    C# List<string>和ArrayList用指定的分隔符分隔成字符串
    自定义可视化调试工具(Microsoft.VisualStudio.DebuggerVisualizers)
    查看SQLServer最耗资源时间的SQL语句
    程序员不适合创业
    如何写高质量,不繁琐的会议记录?
    C#中的Attribute详解(下)
    微信小程序教程系列
  • 原文地址:https://www.cnblogs.com/superrunner/p/10165248.html
Copyright © 2011-2022 走看看