Ch1-5: Write a method to replace all spaces in a string with ‘%20’.
来源:互联网 发布:php substr函数 编辑:程序博客网 时间:2024/05/17 05:51
在剑指offer里面有提到这道题:Write a method to replace all spaces in a string with ‘%20’. 原因是server不能识别空格或者一些字符,所以将data send 给server前要将空格换成一些能识别字符(就像正则一样,有时候为了以后的模式识别,故意增加一些换行,---,%20之类的features,作为flag方便以后提取)。
#include <iostream>#include <cstring>using namespace std;char* replace1(char* c){ if(c == NULL) return NULL; int len = strlen(c); if (len == 0 ) return NULL; int cnt; for (int i=0; i< len; ++i){ if (c[i]==' '){ ++cnt; } } char* cc = new char[len + 2*cnt+1]; int p=0; for (int i=0; i < len; ++i){ if(c[i]==' '){ cc[p] = '%'; cc[p+1]='2'; cc[p+2]='0'; p += 3; } else{ cc[p++]=c[i]; } } cc[p] = '\0'; return cc;}int main(){ char hw[] = "Hello World !"; cout << hw << endl; int len = strlen(hw); cout << len << endl; //char hw1[] = ""; //hw1 = replace1(hw); cout << replace1(hw)<< endl; return 0;}
还有另一个解法是当原来字符串足够大的时候,应该从后往前修改而不要新开一个cc[]空间。但是为什么要从后往前呢?
Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
Executing the program....
$demo
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
Executing the program....
$demo
Hello World !16Hello%20%20%20World%20%20!Hello World !Hello%20%20%20World%20%20!
0 0
- Ch1-5: Write a method to replace all spaces in a string with ‘%20’.
- Write a method to replace all spaces in a string with ‘%20’.
- Write a method to replace all spaces in a string with ‘%20’
- Q1.4 Write a method to replace all spaces in a string with ‘%20’.
- 【5】Replace all spaces in a string with ‘%20’
- Write a method to compute all permutations of a string
- Replace all spaces in string with ' ' (Cracking the Code Interview)
- Q9.5 Write a method to compute all permutations of a string
- CH1-3: remove duplicate char in a string, with/without additional buffer
- How to respond with HTTP 400 error in a Spring MVC @ResponseBody method returning String?
- Write a method that counts the number of occurrences of a given character in a string
- Write a method that returns all subsets of a set.
- Write a method that returns all subsets of a set
- Ch8.6: write a method to paint-fill a closed ring with the same color
- Write a C program to print all permutations of a given string
- Write a program to print all permutations of a given string
- Find All Anagrams in a String Add to List
- define a static method to convert datatime to a formatted string with uesr-defined
- Linux c编程一站式学习笔记(02), Return语句
- Ruby命令行参数介绍
- [LeetCode] Unique Binary Search Trees
- DP34 流水线调度问题 Assembly Line Scheduling @geeksforgeeks
- fwnx - 4.7 tree contains ; recursive
- Ch1-5: Write a method to replace all spaces in a string with ‘%20’.
- fwnx- find the common ancestor in a tree
- [LeetCode] Spiral Matrix
- cocos2d-x游戏开发系列教程-中国象棋06-游戏规则
- [LeetCode] Minimum Path Sum
- IRP IO_STACK_LOCATION 《寒江独钓》内核学习笔记(1)
- DP35 最长等差数列 Longest Arithmetic Progression @geeksforgeeks
- 从网络获取图片方法
- Ch1.6: IN-place-ly rotate a pic(NxN matrix) by 90 digree in Verilog(CPP)