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

     1 /*************************************************************************
     2     > File Name: 02_Replace_Blank.c
     3     > Author: Juntaran
     4     > Mail: JuntaranMail@gmail.com
     5     > Created Time: 2016年08月24日 星期三 01时28分33秒
     6  ************************************************************************/
     7 
     8 #include <stdio.h>
     9 
    10 void ReplaceBlank(char* str)
    11 {
    12     if (str==NULL)
    13     {
    14         return;
    15     }
    16     
    17     int blankCount = 0;
    18     int alphaCount = 0;
    19     int i = 0;
    20     while (str[i] != '')
    21     {
    22         if (str[i] == ' ')
    23             blankCount ++;
    24         alphaCount ++;
    25         
    26         i ++;
    27     }
    28     printf("alphaCount is %d
    ", alphaCount);
    29     printf("blankCount is %d
    ", blankCount);
    30     
    31     int newLength = alphaCount + 2*blankCount;
    32     printf("newLength is %d
    ", newLength);
    33     int p1 = alphaCount + 1;
    34     int p2 = newLength + 1;
    35     while (p1>=0 && p2>p1)
    36     {
    37         if (str[p1] == ' ')
    38         {
    39             str[p2--] = '0';
    40             str[p2--] = '2';
    41             str[p2--] = '%';
    42         }
    43         else
    44         {
    45             str[p2--] = str[p1];
    46         }
    47         p1 --;
    48     }
    49 }
    50 
    51 int main()
    52 {
    53     char str[] = "We are Happy!";
    54     printf("Before: %s
    ", str);
    55     ReplaceBlank(str);
    56     printf("After: %s
    ", str);
    57 }
  • 相关阅读:
    POJ1606 Jugs
    NYOJ148 fibonacci数列(二)
    NYOJ 82 迷宫寻宝(一)
    POJ1579 Function Run Fun
    NYOJ21 三个水杯
    [WorldWind学习]16.Lod技术(1)
    统计推断和统计决策
    程序员,有点累!
    [WorldWind学习]17.视域调度(视域体裁剪)
    c#调用非托管代码
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5801416.html
Copyright © 2011-2022 走看看