%20替换空格

来源:互联网 发布:域名访问量查询 编辑:程序博客网 时间:2024/05/21 10:48
  1. #include<iostream> 
  2. #include<cstdio> 
  3. using namespace std; 
  4.   
  5. int main() 
  6.     char str[1000000]; 
  7.     gets(str); 
  8.   
  9.   
  10.     if("" == str) 
  11.         return 0; 
  12.   
  13.     int len = 0; 
  14.     int blanknum = 0; 
  15.     int i = 0; 
  16.   
  17.     while(str[i] != '\0'
  18.     { 
  19.         len++; 
  20.         if(str[i] == ' '
  21.             blanknum++; 
  22.         i++;     
  23.     } 
  24.   
  25.     int index_from = len + 1; 
  26.     int index_to = len + 1 + 2*blanknum; 
  27.   
  28.     while((index_from>=0) && (index_to>index_from)) 
  29.     { 
  30.         if(str[index_from] ==' '
  31.         { 
  32.             str[index_to--] = '0'
  33.             str[index_to--] = '2'
  34.             str[index_to--] = '%'
  35.             index_from--; 
  36.         } 
  37.         else 
  38.             str[index_to--] = str[index_from--]; 
  39.     } 
  40.   
  41.     printf("%s",str); 
  42.     return 1; 
  43.   
0 0
原创粉丝点击