• *** 自写代码:实现字符串循环右移


    #include <iostream>
    #include <string.h>
    using namespace std;
    // not using string library
    char * rightLoop1 (char * src, int n)
    {
        if (src==NULL) return NULL;
        char * p = src;
        while (*p++);
        int len = p-1-src;
        n %= len;
        char * buff = new char [n];
        // move the overflowed into buffer
        for (int i=0; i<n; i++)
        {
            buff[i] = src[len-n+i];
        }
        // move the sring to the end
        for (int i=len-n-1; i>=0; i--)
        {
            src[i+n] = src[i];
        }
        // copy back from the buffer
        for (int i=0; i<n; i++)
        {
            src[i] = buff[i];
        }
        delete [] buff;
        return src;
    }
    // using strlen and memcpy
    char * rightLoop2 (char * src, int n)
    {
        if (src==NULL) return NULL;
        int len = strlen(src);
        n %= len;
        char * buff = new char [n];
        // move the overflowed into buffer
        memcpy (buff, src+len-n, n);
        // move the sring to the end
        for (int i=len-n-1; i>=0; i--)
        {
            src[i+n] = src[i];
        }
        // copy back from the buffer
        memcpy (src, buff, n);
        delete [] buff;
        return src;
    }
    int main()
    {
        char a[] = "jason_becky";
        cout << rightLoop1 (a, 98) << endl;
        return 0;
    }
  • 相关阅读:
    cookie和session学习笔记
    Listener和Filter学习笔记
    [转载]mysql root密码忘了怎么办
    [转载]oracle连不上的各种现象
    Oauth入门学习
    XML学习笔录
    共享内存
    守护进程
    Linux系统调用与文件I/O(一)
    我的第一篇博客
  • 原文地址:https://www.cnblogs.com/superrunner/p/10165202.html
Copyright © 2020-2023  润新知