zoukankan      html  css  js  c++  java
  • 面试题4:字符串中替换空格

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4249195.html

    题目:把一个字符串中的每个空格替换成"%20",例如:“We are happy”,替换成“We%20are%20happy"。

    解答:首先遍历整个字符串,数一下该字符串一共出现了多少空格,假如出现了n个空格,元字符串长度为len,那么替换后的字符串的长度就为n+len。

    从而可以从后向前的进行移动,定义两个指针,preindex指向原字符串的末尾,aftindex指向替换后字符串的末尾。

    如果preindex指向的值不为空格,则简单复制到aftindex,并preindex--,aftindex--。

    否则的话,在aftindex处从后向前一次添加'0','2','%'然后另,preindex--,aftindex--。

    直到遍历完成个字符串。

    代码如下:

     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 void ReplaceBlank(char string[], int length)
     9 {
    10     if( string == NULL || length <= 0 )
    11     {
    12         return;
    13     }
    14     int spacenum = 0;
    15     int len = strlen(string);
    16     for( int i = 0; i < len ; i++ )
    17     {
    18         if( string[i] == ' ' )
    19         {
    20             spacenum ++ ;
    21         }
    22     }
    23     int newlen = len + spacenum * 2;
    24     if( newlen > length )
    25     {
    26         return;
    27     }
    28     int preindex = len;
    29     int aftindex = newlen; 
    30     for(  ; preindex >= 0 ; preindex-- )
    31     {
    32         if( string[preindex] != ' ' )
    33         {
    34             string[aftindex] = string[preindex];
    35             aftindex --;
    36         }
    37         else
    38         {
    39             string[aftindex--] = '0';
    40             string[aftindex--] = '2';
    41             string[aftindex--] = '%';
    42         }
    43     }
    44 }
    45 
    46 int main(int argc, char *argv[])
    47 {
    48     char string [100];
    49     strcpy(string, "We are Happy");
    50     ReplaceBlank(string, 100);
    51     cout<<string<<endl;
    52 }
    View Code

    另外,如果需要删除掉字符串里的空格,即新的字符串的长度变短,该方法不可行。需要定义两个指针从先向后遍历字符串即可,代码可参考:

     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 void RemoveBlank(char* string)
     9 {
    10     if( string == NULL )
    11     {
    12         return;
    13     }
    14     int i = 0;
    15     int j = 0;
    16     int n = strlen(string);
    17     while(i <= n)
    18     {
    19         if( string[i] != ' ' )
    20         {
    21             string[j] = string[i];
    22             j++;
    23         }
    24         i++;
    25     }
    26 }
    27 
    28 int main(int argc, char *argv[])
    29 {
    30     char string[20] = "We are happy";
    31     RemoveBlank(string);
    32     cout<<string<<endl;
    33 }
    View Code
  • 相关阅读:
    JSP自定义标签
    Java集合之Arrays 剖析
    关于Java8中的Comparator那些事
    关于Comparable和Comparator那些事
    浅析Thread的join() 方法
    多线程的具体实现
    如何实现 List 集合的线程安全
    集合使用 Iterator 删除元素
    Tomcat目录详解
    一文读懂微服务架构
  • 原文地址:https://www.cnblogs.com/jostree/p/4249195.html
Copyright © 2011-2022 走看看