一,c/c++字符串

1.C/C++中每个字符串都以字符’\0‘作为结尾,这样我们就能很方便地找到字符串的最后尾部。

由于这个原因每个字符串都有一个额外的开销,注意字符串越界的问题;

2.C/C++内存模型把字符串常量放到单独的一个内存区域;

当几个指针指向相同的字符串常量的时候,他们实际上会指向常量区那个的内存地址;

但是用字符串常量初始化数组,情况却不一样,这点很重要,考察你C能力的筹码;

test.c:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
#include <stdio.h>

int main()
{
    char str1[] = "hello world";
    char str2[] = "hello world";

char *str3 = "hello boy";
    char *str4 = "hello boy";

if(str1 == str2)
    {
        printf("str1 and str2 are same.\n");
    }
    else
    {
        printf("str1 and str2 are not same\n");
    }

if (str3 == str4)
    {
        printf("str3 and str4 are same.\n");
    }
    else
    {
        printf("str3 and str4 are not same.\n");
    }
    return 0;
}

运行结果:

str1 and str2 are not same
str3 and str4 are same.

Makefile:

1
2
3
4
5
6
7
8
9
10
11
12
 
.PHONY:clean  
CC=gcc  
CFLAGS=-Wall -g  
BIN=test  
OBJS=test.o  
LIBS=  
$(BIN):$(OBJS)  
    $(CC) $(CFLAGS) $^ -o $@ $(LIBS)  
%.o:%.c  
    $(CC) $(CFLAGS) -c $< -o $@  
clean:  
    rm -f *.o $(BIN)  

str1和str2是两个字符串数组,我们会为他们分配两个长度为12个字节的空间(在栈区),

并且把常量区的“hello world”的内容分别拷贝的数组当中。

这是两个初始地址不同的数组;

str3和str4是两个指针,我们无须为她们分配内存来存储字符串的内容,而只需要把他们指向“hello boy”在常量区中的地址就可以了,“hello world”这个字符串常量在内存中只有一个拷贝,因此str3与str4的值是一样的。

二,替换空格

给定字符串中的空格替换成 ’%20‘

思路就是计算出替换后的字符串的长度,利用两个指针,一个指向就字符串的末尾,一个指向新字符串的末尾;

进而从后往前面遍历,这样子节约时间,移位的效率高,因为没有做多余的移位操作;

space.cpp:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

/*length 为字符数组string的总的容量*/
void ReplaceBlank(char string[], int length)
{

if(string == NULL && length <= 0)
    {
        return;
    }

/*originalLength为字符串string的实际长度*/
    int originalLength = 0;
    int numberOfBlank = 0;
    int i = 0;
    while(string[i] != '\0')
    {
        ++ originalLength;

if(string[i] == ' ')
        {
            ++ numberOfBlank;
        }

++ i;
    }

/*newLength为把空格替换成‘%20’后的长度*/
    int newLength = originalLength + numberOfBlank * 2;
    if (newLength > length)
    {
        return ;
    }

int indexOforiginal = originalLength;
    int indexOfNew = newLength;
    while(indexOforiginal >= 0 && indexOfNew > indexOforiginal)
    {
        if(string[indexOforiginal] == ' ')
        {
            string[indexOfNew --] = '0';
            string[indexOfNew --] = '2';
            string[indexOfNew --] = '%';
        }
        else
        {
            string[indexOfNew --] = string[indexOforiginal];
        }
        -- indexOforiginal;
    }

}

void Test(char *testName, char string[], int length, char expected[])
{
    if(testName != NULL)
        printf("%s begins: ", testName);

ReplaceBlank(string, length);

if(expected == NULL && string == NULL)
    {
        cout << "passed." << endl;
    }
    else if(expected == NULL && string != NULL)
    {
        cout << "failed." << endl;
    }
    else if(strcmp(string, expected) == 0)
    {
        cout << "passed." << endl;
    }
    else
    {
        cout << "failed." << endl;
    }
}

int main()
{
    const int length = 100;

char string[length] = "hello world";
    char expected[] = "hello%20world";

ReplaceBlank(string, length);

if(strcmp(string, expected) == 0)
    {
        cout << "passed." << endl;
    }
    else
    {
        cout << "failed." << endl;
    }

return 0;
}

运行结果:

passed.  

Makefile:

1
2
3
4
5
6
7
8
9
10
11
12
 
.PHONY:clean  
CPP=g++  
CFLAGS=-Wall -g  
BIN=test  
OBJS=space.o  
LIBS=  
$(BIN):$(OBJS)  
    $(CPP) $(CFLAGS) $^ -o $@ $(LIBS)  
%.o:%.cpp  
    $(CPP) $(CFLAGS) -c $< -o $@  
clean:  
    rm -f *.o $(BIN)Test1 begins: passed.  

【面试题004】c/c++字符串,替换空格的更多相关文章

  1. 【C语言】字符串替换空格:实现一个函数,把字符串里的空格替换成“%20”

    //字符串替换空格:实现一个函数,把字符串里的空格替换成"%20" #include <stdio.h> #include <assert.h> void ...

  2. 【c语言】字符串替换空格:请实现一个函数,把字符串中的每一个空格替换成“%20”

    // 字符串替换空格:请实现一个函数,把字符串中的每一个空格替换成"%20". // 比如输入"we are happy.",则输出"we%20are ...

  3. 剑指offer 2.字符串 替换空格

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. public class Re ...

  4. 【Offer】[5] 【替换空格】

    题目描述 思路分析 Java代码 代码链接 题目描述 请实现一个函数,把字符串中的每个空格替换成"%20". 例如输入"We are happy.",则输出&q ...

  5. 《剑指offer面试题4》替换空格——实现函数把字符串中每个空格替换成“%20”

    思路: 例如把we are happy这个字符串中所有空格替换成"%20",最直接的做法是从头开始扫苗,遇到空格就替换,并且把空格后面的字符都顺序后移.复杂度O(n^2). 重要思 ...

  6. 剑指Offer:面试题4——替换空格(java实现)

    问题描述:请实现一个函数,把字符串中的每个空格替换成"%20". 例如: 输入:"We are happy." 输出:"We%20are%20happ ...

  7. C++版 - 剑指offer 面试题4: 替换空格 题解

    面试题4:替换空格 提交网址: http://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=1 ...

  8. 剑指offer编程题Java实现——面试题4替换空格

    题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. package Solution; ...

  9. 【剑指Offer面试题】 九度OJ1510:替换空格

    c/c++ 中的字符串以"\0"作为结尾符.这样每一个字符串都有一个额外字符的开销. 以下代码将造成内存越界. char str[10]; strcpy(str, "01 ...

随机推荐

  1. 《HTML5与CSS3基础教程》学习笔记 ——Four Day

    第十六章 1.    输入和元素 电子邮件框 <input type="email"> 搜索框 <input type="search"> ...

  2. Essential C++ 学习笔记02--Array/Vector 与指针

    Essential C++ 1.5-1.6节,3.1节笔记 Array/Vector/指针,难度偏大, 但若学习初期不熟悉基本用法,则难以写出有效代码. 1. 基本概念 Array 是一段连续内存,数 ...

  3. unity 多线程

    对于客户端来说,好的用户体验,需要保持一个快速响应的用户界面.于是便要求:网络请求.io操作等 开销比较大的操作必须在后台线程进行,从而避免主线程的ui卡顿.(注:协程也是主线程的一部分,进行大量的i ...

  4. 解决ubuntu 14.04 下eclipse 3.7.2 不能启动,报Could not detect registered XULRunner to use 或 org.eclipse.swt.SWTError: XPCOM 等问题的处理

    对于eclipse 3.7.2在ubuntu 14.04下不能启动,需要在 eclipse/configuration 目录下的config.ini文件内增加一行org.eclipse.swt.bro ...

  5. linux编程:环境表

    每个进程在启动的时候都会收到一张环境表.环境表是由一个字符指针数组组成,每个指针包含一个以NULL结束的字符串的地址,全局变量environ包含了指针数组的地址: extern char **envi ...

  6. HTML5 Shiv – 让该死的IE系列支持HTML5吧

    HTML5能为我们做的事儿很多,最为可口的就是语义化标签的应用,如果你已经在Chrome或者其他支持HTML5的浏览器上用过它的牛x,那这篇文章对你一定有用,因为现在你也可以在IE上用到HTML5. ...

  7. webview的弹性布局之rem,em

    webview页面的自适应一般有两种方法,即一是JS的计算方法,二是通过css的media设置分档方式.在此主要介绍css的方式. html { font-size: 16px; } @media o ...

  8. [react native] Error loading page

    如上图显示的错误,解决方法如下: 在react native ios项目的info.plist文件中,新增一个属性. 在Info.plist中添加NSAppTransportSecurity类型Dic ...

  9. php微信开发(1):缓存access_token的方法

    语言:PHP access_token一直要用,但每天取的数量有限制.反正2小时才过期.就想缓存一下. File1: wx_access_token.php File2: file_cache.php ...

  10. 一个订单相关的存储过程(MySQL)

    BEGIN DECLARE currentDate VARCHAR(15) ;/*当前日期,有可能包含时分秒 */ DECLARE maxNo INT DEFAULT 0 ; /* 离现在最近的满足条 ...