码迷,mamicode.com
首页 > 编程语言 > 详细

C++字符串操作笔试题第二波

时间:2015-07-03 17:31:14      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:字符串笔试题


//1.字符串替换空格:请实现一个函数,把字符串中的每个空格替换成“%20”。
//例如输入“we are happy.”,则输出“we%20are%20happy.”。
#include <iostream>
#include <assert.h>
#include <string.h>
using namespace std;

char* Grial(char *s)
{
    assert(s != NULL);
    int len = strlen(s);
    int count = 0;//计数空格数。
    char *p = s;
    while (*p != ‘\0‘)
    {
        if (*p == ‘ ‘)count++;
        p++;
    }
    int n = len + count * 2 + 1;
    char *str = new char[n];
    char *ret = str;
    memset(str,‘\0‘,n);
    strcpy(str,s);//将原来的字符串拷贝到新的字符串串数组中。
    p = str + n - 1;
    char *q = str + len;
    while (q < p)
    {
        if (*q == ‘ ‘)
        {
            *p-- = ‘0‘;
            *p-- = ‘2‘;
            *p = ‘%‘;
        }
        else
        {
            *p = *q;
        }
        q--;
        p--;
    }
    return ret; 
}
int main()
{
    char s[] = "we are happy";
    cout<<Grial(s)<<endl;
    return 0;
}


//2.判断一个字符串是否为另外一个字符串旋转之后的字符串。
//例如:给定s1 = AABCD和s2 = BCDAA,返回1,给定s1 = abcd
//和s2 = ACBD,返回0.

#include <iostream>
#include <assert.h>
using namespace std;
bool Grial(const char *str1,const char *str2)
{
    assert(str1!=NULL&&str2!=NULL);
    const char *p = str1;
    const char *q = str2 + strlen(str2) - 1;
    if (strlen(str1) != strlen(str2))return 0;
    while (*p != ‘\0‘)
    {
        if (*p != *q)return 0;
        p++;
        q--;
    }
    return 1;
}
int main()
{
    char s1[] = "abcd";
    char s2[] = "dcba";
    cout << Grial(s1, s2) << endl;
    return 0;
}*/

//3.定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部。
//如把字符串 abcdef 左旋转 2 位得到字符串 cdefab。请实现字符串左旋转的函数。
//要求时间对长度为 n 的字符串操作的复杂度 为 O(n), 辅助内存为 O(1)。
#include <iostream>
#include <assert.h>
using namespace std;
void Swap(char *p1,char *p2)
{
    char temp;
    while (p1 < p2)
    {
        temp = *p1;
        *p1 = *p2;
        *p2 = temp;
        p1++;
        p2--;
    }
}
void Grial(char *s,int n)
{
    assert(s!=NULL);
    int len = strlen(s);
    char *p = s + len - 1;
    char *q = s;
    char *midchar = NULL;   
    Swap(q,p);//先整体逆序。
    midchar = p - n+1;//然后分成两部分逆序。
    Swap(q,midchar-1);
    Swap(midchar,p);
}
int main()
{
    char s[] = "abcdef";
    Grial(s, 3);
    cout << s << endl;
    return 0;
}*/

//4.查找子字符串:模拟strstr函数的实现。
#include <iostream>
#include <assert.h>
#include <string.h>
using namespace std;
int my_atoi(char *p1,char *p2)
{
    int count = 0;
    while (p1 <= p2)
    {
        count = count * 10 + *p1 - ‘0‘;
        p1++;
    }
    return count;
}
char * my_strstr(char *dist, char *src)
{
    int n = strlen(src);
    int num1 = my_atoi(src,src+n-1);
    int flags = num1 % 13;//标记。
    char *p = dist;
    char *q;
    char *m;
    while (p <= dist + strlen(dist) - n)
    {
        m=p;
        if (*p == *src)
        {
            if (my_atoi(p, p + n - 1) % 13 == flags)
            {
                q = src;
                while (*q != ‘\0‘ && *p!=‘\0‘)
                {
                    if (*q != *p)break;
                    q++;
                    p++;
                }
                if (*q == ‘\0‘)
                    return m;
            }   
        }
        m++;
        p=m;

    }
    return NULL;

}

int main()
{
    char s1[] = "123456789";
    char s2[] = "789";

    cout<<my_strstr(s1, s2)<<endl;

    return 0;
}







//5.模拟实现库函数的atoi函数。
#include <iostream>
#include <assert.h>
using namespace std;
bool IsNum(char ch)
{
    if ((ch - ‘0‘) >= 0 && (ch - ‘0‘) <= 9)return true;
    return false;
}
int GetNum(const char *s)
{
    const char *p = s;
    int count = 0;
    while (*p != ‘\0‘)
    {
        if (*p == ‘.‘)return count;
        if (count<0 )return 2147483647;
        count = count * 10 + *p - ‘0‘;
        p++;
    }
}

int my_atoi(const char *s)
{
    assert(s != NULL);
    char ch = *s;
    char flags;
    if (IsNum(ch))
        flags = IsNum(ch) + ‘0‘;
    else
        flags = ch;
    const char *p = s + 1;

    switch (flags)
    {
    case‘+‘:
        return GetNum(p);
        break;
    case ‘-‘:
        if (GetNum(p) == 2147483647)
            return 0 - GetNum(p) - 1;
        return 0 - GetNum(p);
        break;
    case ‘1‘:
        return GetNum(s);
        break;
    default:
        exit(2);
    }
    return 0;
}
int main()
{
    cout << my_atoi("-31321312321321") << endl;
    return 0;
}




#include <iostream>
#include <assert.h>
using namespace std;
//自己实现一个memmove,深入考虑内存覆盖问题。
void* my_memmove(void *dist,void *src,int len)
{
    assert(dist!=NULL&&src!=NULL);
    char *pdist = (char *)dist;
    void *ret = dist;
    char *psrc = (char *)src;
    if (pdist <= psrc || psrc + len <= pdist)
    {
        while (len--)
        {
            *pdist++ = *psrc++;
        }
        return ret;
    }
    else
    {
        pdist += len - 1;
        psrc += len - 1;
        while (len--)
        {
            *pdist-- = *psrc--;
        }
        return ret;
    }
}
int main()
{
    int a[] = { 2, 3, 4, 65, 1, 6, 7 };
    my_memmove(a+1,a,8);
    int *b = new int[7];
    my_memmove(b,a,16);
    int i = 0;
    for (; i < 4; i++)
    {
        cout << b[i] << "  ";
    }
    int i = 0;
    for (; i < 7; i++)
    {
        cout << a[i] << "  ";
    }
    cout << endl;
    return 0;
}



#include <iostream>
#include <assert.h>
using namespace std;

void my_memcopy(void *a,const  void *b, int len)
{
    assert(a != NULL&&b != NULL);
    char *p = (char *)a;
    char *q = (char *)b;
    while (len--)
    {
        *p++ = *q++;
    }
}

int main()
{
    int a[] = { 1, 2, 3, 4, 5 };
    int *b = new int[5];
    my_memcopy(b, a, sizeof(a));
    int i = 0;
    for (; i < 5; i++)
    {
        cout << b[i] << endl;
    }
    memcpy(b,a,100);
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

C++字符串操作笔试题第二波

标签:字符串笔试题

原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/46743229

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!