zoukankan      html  css  js  c++  java
  • 替换空格

     1 #include "stdafx.h"
     2 #include <iostream>
     3 #include <exception>
     4 using namespace std;
     5 
     6 /*替换空格*/
     7 /*
     8 题目:请实现一个函数,把字符串中的每个空格替换成"%20".例如输入"We are happy",则输出"We%20are%20happy".
     9 */
    10 int ReplaceBlank(char string[],int length)
    11 {
    12     int i = 0;
    13     int blankNum=0;
    14     int strLength=0;
    15     while(string[i]!='')
    16     {
    17         strLength++;
    18         if(string[i] == ' ')
    19         {
    20             blankNum++;
    21         }
    22         ++i;
    23     }
    24     int endIndex = strLength+blankNum*2;
    25     if(endIndex > 99)
    26     {
    27         cout<<"数组长度不够!"<<endl;
    28         return 0;
    29     }
    30     while(strLength>=0 && endIndex>strLength)
    31     {
    32         if(string[strLength] != ' '){
    33             string[endIndex] = string[strLength];
    34             endIndex--;
    35         }
    36         else
    37         {
    38             string[endIndex--]='0';
    39             string[endIndex--]='2';
    40             string[endIndex--]='%';
    41         }
    42         strLength--;
    43     }
    44     return 1;
    45 
    46 } 
    47 int _tmain(int argc, _TCHAR* argv[])
    48 { 
    49     char str[100]="hello w o  rld";
    50     ReplaceBlank(str,100);
    51     cout<<str<<endl;
    52     return 0 ;
    53 }
  • 相关阅读:
    算法(第四版)2.1 初级排序算法
    数据类型:数值
    数据类型:null, undefined 和布尔值
    数据类型:概述
    9.6 http中间件
    9.5 处理http 请求
    9.4 简单httpserver
    9.3 多客户端TCP
    9.2 udp server
    资源竞争
  • 原文地址:https://www.cnblogs.com/crazycodehzp/p/3556585.html
Copyright © 2011-2022 走看看