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 }
  • 相关阅读:
    Ubuntu 12.10使用apt安装Oracle/Sun JDK
    织梦(dedecms)系统常用全局变量调用标签及路径
    Lighttpd虚拟主机和多域名的配置
    Ubuntu解压命令大全
    OFBiz终于起航了
    eclipse 安装gradle 插件的三种方式
    验证码
    session的使用
    实验二
    作业2(魔术)
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5801416.html
Copyright © 2011-2022 走看看