Home Database Mysql Tutorial 面试题2:替换空格

面试题2:替换空格

Jun 07, 2016 pm 03:17 PM
accomplish replace source space test questions

来源:《剑指Offer》 题目:请实现一个函数,把字符串的每个空格替换成”%20”,原字符串后面有足够的空间,要求在原字符串上操作。例如输入”we are happy”,则输出”we%20are%20happy”。 分析:先遍历一遍统计出空格数,一个空格替换成三个字符,长度增

来源:《剑指Offer》

题目:请实现一个函数,把字符串的每个空格替换成”%20”,原字符串后面有足够的空间,要求在原字符串上操作。例如输入”we are happy”,则输出”we%20are%20happy”。

分析:先遍历一遍统计出空格数,一个空格替换成三个字符,长度增加了2,N个空格,长度增加2N。则
替换后的字符串长度 = 原字符串长度 + 2N
设置两个指针,一个指向原字符串末尾,一个指向新字符串末尾。第二遍遍历,从后往前遍历。将原字符串最后一个字符复制到新字符串最后一个字符的位置,两个指针同时前移。当原字符串遇到空格,在新字符串对应位置插入”%20”三个字符,接着原字符串指针前移1位,新字符串指针前移3位。如此操作,直到原字符串第一个字符也复制完结束。

代码(C++):

<code><span>#include<iostream></iostream></span>
<span>using</span> <span>namespace</span> <span>std</span>;

<span>#define MAX_LEN 100</span>

<span>/*
    函数功能:替换字符串中的空格
  */</span>
<span>void</span> replaceBlank(<span>char</span> data[])
{
    <span>int</span> i=<span>0</span>,j;
    <span>int</span> old_len;
    <span>int</span> new_len;
    <span>int</span> blank_num=<span>0</span>;
    <span>// 遍历一遍,找出空格数</span>
    <span>while</span>(data[i]!=<span>'\0'</span>)
    {
        <span>if</span>(data[i]==<span>' '</span>)
        {
            blank_num++;
        }
        i++;
    }
    <span>//-----------这里是关键------------</span>

    <span>// 原来字符串长度</span>
    old_len=i+<span>1</span>;
    <span>// 计算新字符串长度</span>
    new_len=old_len+<span>2</span>*blank_num;
    <span>// 重置i,j</span>
    i=old_len-<span>1</span>;
    j=new_len - <span>1</span>;

    <span>//-----------这里是关键------------</span>


    <span>// 再遍历一遍,替换空格</span>
    <span>while</span>(i>=<span>0</span>)
    {
        <span>if</span>(data[i]!=<span>' '</span>)
        {
            data[j]=data[i];        
            j--;
        }
        <span>else</span>
        {
            data[j]=<span>'0'</span>;
            data[j-<span>1</span>]=<span>'2'</span>;
            data[j-<span>2</span>]=<span>'%'</span>;
            j-=<span>3</span>;
        }
        i--;
    }

}
<span>int</span> main()
{
    <span>char</span> str[MAX_LEN];<span>// 声明一个字符数组</span>

    <span>// 输入数组</span>
    <span>cout</span>"请输入一个字符串,不超过20个字符"// 替换
    replaceBlank(str);
    <span>// 打印</span>
    <span>cout</span>return <span>0</span>;
}

</code>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

The difference between full-width spaces and half-width spaces The difference between full-width spaces and half-width spaces Mar 25, 2024 pm 12:45 PM

The difference between full-width spaces and half-width spaces. When we use word processing software or edit text content, we sometimes encounter the concept of spaces. Space is a very basic element in typesetting and formatting text, but many people may not know the difference between full-width spaces and half-width spaces. In daily use, we may feel that full-width spaces and half-width spaces have different effects in different situations, but we may not be aware of the subtle differences. First of all, the difference between full-width spaces and half-width spaces is the width they occupy.

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

PyCharm Beginner's Guide: Comprehensive Analysis of Replacement Functions PyCharm Beginner's Guide: Comprehensive Analysis of Replacement Functions Feb 25, 2024 am 11:15 AM

PyCharm is a powerful Python integrated development environment with rich functions and tools that can greatly improve development efficiency. Among them, the replacement function is one of the functions frequently used in the development process, which can help developers quickly modify the code and improve the code quality. This article will introduce PyCharm's replacement function in detail, combined with specific code examples, to help novices better master and use this function. Introduction to the replacement function PyCharm's replacement function can help developers quickly replace specified text in the code

See all articles