www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Replace Spaces With Characters

Last update: 10/26/2018 6:06:00 AM

Written by:Fatih KABAKCI

The question is to replace all spaces in a given string with a special characters. In this question, we are asked to not to use extra space to edit the string such as transferring characters to a new array or string. Use same string to solve the problem.

Hint:Since strings are immutable, we should use char arrays to edit characters without using an extra storage. However, we need to remember to give extra spaces in end of the string to be able to manipulate the characters by moving to the end.

The strategy is to start from end and move the characters through right in the char array. If there is a space encountered, then add special characters in reverse order.

package com.fatihkabakci.arrStr;

/**
 * 
 * @author fkabakci 
 * Replace all spaces with the character %20. Use same char array. No extra space is used.
 */
public class ReplaceSpaceWithCharacters {
	private static int getNumberOfSpaces(char[] str, int length) {
		int numberOfSpaces = 0;
		for(int i = 0; i < length; i++) {
			if(str[i] == " ") {
				numberOfSpaces++;
			}
		}
		return numberOfSpaces;
	}
	
	public static void replaceAllSpaces(char[] str, int length) {
		int numberOfSpaces = getNumberOfSpaces(str, length);		
		int arrIndex = numberOfSpaces * 2 + length - 1;
		for(int i = length - 1; i >= 0 ; i--) {
			if(str[i] == " ") {
				str[arrIndex--] = "0";
				str[arrIndex--] = "2";
				str[arrIndex--] = "%";
				
			}else {
				str[arrIndex--] = str[i];
			}
		}
	}
	public static void main(String[] args) {
		// extra space at the end of the string has to be !
		char[] input = "Fatih SF USA    ".toCharArray();
		replaceAllSpaces(input, 12);
		System.out.println(input);
	}
}

The algorithm is O(n) complexity.

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.