[Sword refers to Offer Interview Questions] 9-degree OJ1510: Replace space, offeroj1510

Source: Internet
Author: User
Tags first string

[Sword refers to Offer Interview Questions] 9-degree OJ1510: Replace space, offeroj1510

The string in c/c ++ uses "\ 0" as the end characterIn this way, each string has an additional character overhead. The following code will cause memory out of bounds.
char str[10];
strcpy(str, “0123456789”);

To save memory, c/c ++ places the constant string in a separate memory area.When several pointers give the same constant string, they actually point to the same memory address.

Question link:
Http://ac.jobdu.com/problem.php? Pid = 1, 1510

Replace Spaces

Time Limit: 1 second memory limit: 128 MB Special Judgment: No submitted: 9648 resolution: 2483
Description:
Implement a function to replace spaces in a string with "% 20 ". For example, if the string is We Are Happy, the replaced string is We % 20Are % 20 Happy.
Input:
Each input file contains only one set of test samples.
For each group of test cases, enter a line to indicate the strings to be processed.
Output:
Corresponding to each test case, the processed string is output.
Sample input:
We Are Happy
Sample output:
We % 20Are % 20 Happy

The analysis in sword finger offer is very detailed:
1. If you look for a space from the front to the back, move the following characters after the space, so that the subsequent characters will be moved multiple times.
2. If the number of spaces is calculated first, the character can be moved only once. Learn how to change your mind.

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; void ReplaceBlank (char * str) {if (str = NULL) return; int len = strlen (str ); // when the first string Terminator '\ 0' is reached, the return counter value (length does not include "\ 0") if (len = 0) return; int I, count = 0; for (I = 0; I
    
     
= 0 & indexOfnew_len> indexOflen) {if (str [indexOflen] = '') {str [indexOfnew_len --] = '0 '; str [indexOfnew_len --] = '2'; str [indexOfnew_len --] = '%';} else {str [indexOfnew_len --] = str [indexOflen];} -- indexOflen ;}} int main () {static char str [10000002]; gets (str); ReplaceBlank (str); puts (str); return 0 ;} /*************************************** * *********************** Problem: 1510 Language: C ++ Result: Accepted Time: 20 MS Memory: 11284 kb ************************************** * ***********************/"data-snippet-id =" ext.7ca15b8f8c8d30bf0e0c99007bdad244 "data-snippet -saved = "false" data-csrftoken = "SV45t05l-bal0EA4_VBXU_a8ZNUyWv1nEsjk" data-codota-status = "done">
     /******************************** ------------------------------- [Sword refers to the offline interview questions] Replace space ----------------------------------- Author: mu zhidian Date: 2015 Email: bzhou84@163.com **********************************/# include <stdio. h >#include <cstring >#include <string ># include <iostream> using namespace std; void ReplaceBlank (char * str) {if (str = NULL) return; int len = strlen (str); // when the first string Terminator '\ 0' is reached, the counter value is returned (the length does not contain "\ 0 ") if (len = 0) return; int I, count = 0; for (I = 0; I <len; I ++) if (str [I] = '') count ++; // if there is no space, return str if (count = 0) return; int new_len = 2 * count + len; int indexOflen = len; int indexOfnew_len = new_len; while (indexOflen> = 0 & indexOfnew_len> indexOflen) {if (str [indexOflen] = '') {str [indexOfnew_len --] = '0'; str [indexOfnew_len --] = '2 '; str [indexOfnew_len --] = '%';} else {str [indexOfnew_len --] = str [indexOflen];} -- indexOflen;} int main () {static char str [10000002]; gets (str); ReplaceBlank (str); puts (str); return 0 ;} /*************************************** * *********************** Problem: 1510 Language: C ++ Result: Accepted Time: 20 MS Memory: 11284 kb ************************************** **************************/
    
   
  
 

Note:

-Convert string to char *

#include 
 
  using namespace std;int main(){    string str1="Hello";    char *str2=const_cast
  
   (str1.c_str());    cout<
   
    #include <iostream>#include <string>using namespace std;int main(){    string str1="Hello";    char *str2=const_cast<char*>(str1.c_str());    cout<<str2;    return 0;}
   
  
 

-To complete this question, the simple method is to output a non-space question and output "% 20" if it encounters a space ".

  #include 
 
    #include 
  
     using namespace std;  int main()  {      char str[10000002];      while(gets(str))      {             int i=0;          while(str[i]!='\0')          {               if(str[i]==' ') cout<<"%20";               else cout<
   
    #include <cstdio>  #include <iostream>  #include <string>  using namespace std;  int main()  {      char str[10000002];      while(gets(str))      {             int i=0;          while(str[i]!='\0')          {               if(str[i]==' ') cout<<"%20";               else cout<<str[i];               i++;                           }             cout<<endl;                   }         return 0;  }   /**************************************************************    Problem: 1510    Language: C++    Result: Accepted    Time:20 ms    Memory:11212 kb****************************************************************/
   
  
 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.