Move all spaces at the middle of string
Here given code implementation process.
//C Program
//Move all spaces at the middle of string
#include <stdio.h>
//Reverse the string elements of given size
void reverse(char str[],int start,int end)
{
char temp=' ';
for(start;start < end;start++,end--)
{
temp=str[start];
str[start]=str[end];
str[end]=temp;
}
}
void move_space(char str[],int size)
{
int position = 0;
printf("Before : [%s]\n",str );
//move non space character at the beginning position
for (int i = 0; i <size; ++i)
{
if(str[i]!=' ')
{
str[position]=str[i];
position++;
}
}
if(position>0)
{
for (int i = position; i < size; ++i)
{
str[i]=' ';
}
//separate the string elements
reverse(str,position/2,position-1);
reverse(str,position/2,size-1);
}
}
int main()
{
char str1[]="h o w to imp le ment th is alg orit hms";
int size = sizeof(str1)/sizeof(str1[0])-1;
move_space(str1,size);
printf("After : [%s]\n",str1 );
char str2[]="this is simple text message";
size = sizeof(str2)/sizeof(str2[0])-1;
move_space(str2,size);
printf("After : [%s]\n",str2 );
return 0;
}
Output
Before : [h o w to imp le ment th is alg orit hms]
After : [howtoimplement thisalgorithms]
Before : [this is simple text message]
After : [thisissimpl etextmessage]
// Java program
// Move all spaces at the middle of string
public class MyString {
//Swapping two string elements by index
public String swap(String text,int size,int a,int b)
{
//Check valid location of swap element
if((a>=0 && a<size) && (b >= 0 && b < size))
{
//Get first character
char first = text.charAt(a);
//Get second character
char second = text.charAt(b);
//Put character
text = text.substring(0, b)
+ first
+ text.substring(b + 1);
text = text.substring(0, a)
+ second
+ text.substring(a + 1);
}
return text;
}
//Reverse the string elements of given size
public String reverse(String result, int start, int last,int size)
{
for (start=start; start < last; start++, last--)
{
result = swap(result,size,start,last);
}
return result;
}
public String move_space(String text)
{
int size = text.length();
int position = 0;
String result ="";
System.out.print("Before : ["+text+"]\n");
//Move non space character at the beginning position
for (int i = 0; i < size; ++i)
{
if (text.charAt(i) != ' ')
{
result += text.charAt(i);
position++;
}
}
if (position > 0)
{
for (int i = position; i < size; ++i)
{
result += ' ';
}
//separate the string elements
result=reverse(result, position / 2, position - 1,size);
result=reverse(result, position / 2, size - 1,size);
}
return result;
}
public static void main(String[] args)
{
MyString obj = new MyString();
String text = "h o w to imp le ment th is alg orit hms";
text = obj.move_space(text);
System.out.print("After : ["+text+"]\n");
text = "this is simple text message";
text = obj.move_space(text);
System.out.print("After : ["+text+"]\n");
}
}
Output
Before : [h o w to imp le ment th is alg orit hms]
After : [howtoimplement thisalgorithms]
Before : [this is simple text message]
After : [thisissimpl etextmessage]
// C++ program
// Move all spaces at the middle of string
#include<iostream>
using namespace std;
class MyString {
public:
//Swap the string element of given indexes
string swap(string text,int size,int a,int b)
{
if((a>=0 && a<size) && (b >= 0 && b < size))
{
//When valid a and b location
char temp = text[a];
text[a] = text[b];
text[b] = temp;
}
//return modified result
return text;
}
//Reverse the string elements of given size
string reverse(string result, int start, int last, int size) {
for (start = start; start < last; start++, last--) {
result = this->swap(result, size, start, last);
}
return result;
}
string move_space(string text) {
int size = text.size();
int position = 0;
string result = "";
cout << "Before : [" << text << "]\n";
//Move non space character at the beginning position
for (int i = 0; i < size; ++i) {
if (text[i] != ' ') {
result += text[i];
position++;
}
}
if (position > 0) {
for (int i = position; i < size; ++i) {
result += ' ';
}
//separate the string elements
result = this->reverse(result, position / 2, position - 1, size);
result = this->reverse(result, position / 2, size - 1, size);
}
return result;
}
};
int main() {
MyString obj = MyString();
string text = "h o w to imp le ment th is alg orit hms";
text = obj.move_space(text);
cout << "After : [" << text << "]\n";
text = "this is simple text message";
text = obj.move_space(text);
cout << "After : [" << text << "]\n";
return 0;
}
Output
Before : [h o w to imp le ment th is alg orit hms]
After : [howtoimplement thisalgorithms]
Before : [this is simple text message]
After : [thisissimpl etextmessage]
// C# program
// Move all spaces at the middle of string
using System;
public class MyString {
//Swapping two string elements by index
public String swap(String text, int size, int a, int b) {
//Check valid location of swap element
if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
//Get first character
char first = text[a];
//Get second character
char second = text[b];
//Put characters
text = text.Substring(0, b) + first + text.Substring(b + 1);
text = text.Substring(0, a) + second + text.Substring(a + 1);
}
//important this text are modified inside a function.
//return modified text and assign on actual text variable
return text;
}
//Reverse the string elements of given size
public String reverse(String result, int start, int last, int size) {
int i = start;
for (start = i ; start < last; start++, last--) {
result = swap(result, size, start, last);
}
return result;
}
public String move_space(String text) {
int size = text.Length;
int position = 0;
String result = "";
Console.Write("Before : [" + text + "]\n");
//Move non space character at the beginning position
for (int i = 0; i < size; ++i) {
if (text[i] != ' ') {
result += text[i];
position++;
}
}
if (position > 0) {
for (int i = position; i < size; ++i) {
result += ' ';
}
//separate the string elements
result = reverse(result, position / 2, position - 1, size);
result = reverse(result, position / 2, size - 1, size);
}
return result;
}
public static void Main(String[] args) {
MyString obj = new MyString();
String text = "h o w to imp le ment th is alg orit hms";
text = obj.move_space(text);
Console.Write("After : [" + text + "]\n");
text = "this is simple text message";
text = obj.move_space(text);
Console.Write("After : [" + text + "]\n");
}
}
Output
Before : [h o w to imp le ment th is alg orit hms]
After : [howtoimplement thisalgorithms]
Before : [this is simple text message]
After : [thisissimpl etextmessage]
<?php
// Php program
// Move all spaces at the middle of string
class MyString {
//Swapping two string elements by index
public function swap($text, $size, $a, $b) {
//Check valid location of swap element
if (($a >= 0 && $a < $size) && ($b >= 0 && $b < $size)) {
//Get first character
$first = $text[$a];
//Get second character
$second = $text[$b];
//Put character
$text = substr($text,0, $b-strlen($text)) . $first .substr($text,$b+1 );
$text = substr($text,0, $a-strlen($text)) . $second .substr($text,$a+1 );
}
return $text;
}
//Reverse the string elements of given size
public function reverse($result, $start, $last, $size) {
for ($start ; $start < $last; $start++, $last--) {
$result = $this->swap($result, $size, $start, $last);
}
return $result;
}
public function move_space($text) {
$size = strlen($text);
$position = 0;
$result = "";
echo("Before : [". $text ."]\n");
//Move non space character at the beginning position
for ($i = 0; $i < $size; ++$i) {
if ($text[$i] != ' ') {
$result .= $text[$i];
$position++;
}
}
if ($position > 0) {
for ($i = $position; $i < $size; ++$i) {
$result .= ' ';
}
//separate the string elements
$result = $this->reverse($result, intval($position / 2), $position - 1, $size);
$result = $this->reverse($result, intval($position / 2), $size - 1, $size);
}
return $result;
}
}
function main() {
$obj = new MyString();
$text = "h o w to imp le ment th is alg orit hms";
$text = $obj->move_space($text);
echo("After : [". $text ."]\n");
$text = "this is simple text message";
$text = $obj->move_space($text);
echo("After : [". $text ."]\n");
}
main();
Output
Before : [h o w to imp le ment th is alg orit hms]
After : [howtoimplement thisalgorithms]
Before : [this is simple text message]
After : [thisissimpl etextmessage]
// Node Js program
// Move all spaces at the middle of string
class MyString {
//Swapping two string elements by index
swap(text, size, a, b) {
//Check valid location of swap element
if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
//Get first character
var first = text[a];
//Get second character
var second = text[b];
//Put character
text = text.substring(0, b) + first + text.substring(b + 1);
text = text.substring(0, a) + second + text.substring(a + 1);
}
return text;
}
//Reverse the string elements of given size
reverse(result, start, last, size) {
for (start ; start < last; start++, last--) {
result = this.swap(result, size, start, last);
}
return result;
}
move_space(text) {
var size = text.length;
var position = 0;
var result = "";
process.stdout.write("Before : [" + text + "]\n");
//Move non space character at the beginning position
for (var i = 0; i < size; ++i) {
if (text[i] != ' ') {
result += text[i];
position++;
}
}
if (position > 0) {
for (var i = position; i < size; ++i) {
result += ' ';
}
//separate the string elements
result = this.reverse(result, parseInt(position / 2), position - 1, size);
result = this.reverse(result, parseInt(position / 2), size - 1, size);
}
return result;
}
}
function main(args) {
var obj = new MyString();
var text = "h o w to imp le ment th is alg orit hms";
text = obj.move_space(text);
process.stdout.write("After : [" + text + "]\n");
text = "this is simple text message";
text = obj.move_space(text);
process.stdout.write("After : [" + text + "]\n");
}
main();
Output
Before : [h o w to imp le ment th is alg orit hms]
After : [howtoimplement thisalgorithms]
Before : [this is simple text message]
After : [thisissimpl etextmessage]
# Python 3 program
# Move all spaces at the middle of string
class MyString :
# Swapping two string elements by index
def swap(self, text, size, a, b) :
# Check valid location of swap element
if ((a >= 0 and a < size) and(b >= 0 and b < size)) :
data = list(text)
data[a],data[b] = data[b],data[a]
return ''.join(data)
return text
# Reverse the string elements of given size
def reverse(self, result, start, last, size) :
start = start
while (start < last) :
result = self.swap(result, size, start, last)
start += 1
last -= 1
return result
def move_space(self, text) :
size = len(text)
position = 0
result = ""
print("Before : [{}]".format(text))
# Move non space character at the beginning position
i = 0
while (i < size) :
if (text[i] != ' ') :
result += text[i]
position += 1
i += 1
if (position > 0) :
i = position
while (i < size) :
result += ' '
i += 1
# separate the string elements
result = self.reverse(result, int(position / 2), position - 1, size)
result = self.reverse(result, int(position / 2), size - 1, size)
return result
def main() :
obj = MyString()
text = "h o w to imp le ment th is alg orit hms"
text = obj.move_space(text)
print("After : [{}]".format(text))
text = "this is simple text message"
text = obj.move_space(text)
print("After : [{}]".format(text))
if __name__ == "__main__":
main()
Output
Before : [h o w to imp le ment th is alg orit hms]
After : [howtoimplement thisalgorithms]
Before : [this is simple text message]
After : [thisissimpl etextmessage]
# Ruby program
# Move all spaces at the middle of string
class MyString
# Swapping two string elements by index
def swap(text, size, a, b)
# Check valid location of swap element
if ((a >= 0 && a < size) && (b >= 0 && b < size))
# Get first character
first = text[a]
text[a]=text[b]
text[b]=first
end
return text
end
# Reverse the string elements of given size
def reverse(result, start, last, size)
start = start
while (start < last)
result = self.swap(result, size, start, last)
start += 1
last -= 1
end
return result
end
def move_space(text)
size = text.length()
position = 0
result = ""
print("Before : [", text ,"]\n")
# Move non space character at the beginning position
i = 0
while (i < size)
if (text[i] != ' ')
result += text[i]
position += 1
end
i += 1
end
if (position > 0)
i = position
while (i < size)
result += ' '
i += 1
end
# separate the string elements
result = self.reverse(result, position / 2, position - 1, size)
result = self.reverse(result, position / 2, size - 1, size)
end
return result
end
end
def main()
obj = MyString.new()
text = "h o w to imp le ment th is alg orit hms"
text = obj.move_space(text)
print("After : [", text ,"]\n")
text = "this is simple text message"
text = obj.move_space(text)
print("After : [", text ,"]\n")
end
main()
Output
Before : [h o w to imp le ment th is alg orit hms]
After : [howtoimplement thisalgorithms]
Before : [this is simple text message]
After : [thisissimpl etextmessage]
// Scala program
// Move all spaces at the middle of string
class MyString {
//Swapping two string elements by index
def swap(info: String, size: Int, a: Int, b: Int): String = {
//Check valid location of swap element
var text = info;
if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
//Get first character
val first: Char = text(a);
//Get second character
val second: Char = text(b);
//Put character
text = text.substring(0, b) + first + text.substring(b + 1);
text = text.substring(0, a) + second + text.substring(a + 1);
}
return text;
}
//Reverse the string elements of given size
def reverse(data : String, s: Int, e: Int, size: Int): String = {
var start : Int = s;
var last : Int = e;
var result : String = data;
while (start < last) {
result = swap(result, size, start, last);
start += 1;
last -= 1;
}
return result;
}
def move_space(text: String): String = {
var size: Int = text.length();
var position: Int = 0;
var result: String = "";
print("Before : [" + text + "]\n");
//Move non space character at the beginning position
var i: Int = 0;
while (i < size) {
if (text(i) != ' ') {
result += text(i);
position += 1;
}
i += 1;
}
if (position > 0) {
i = position;
while (i < size) {
result += ' ';
i += 1;
}
//separate the string elements
result = reverse(result, (position / 2).toInt, position - 1, size);
result = reverse(result, (position / 2).toInt, size - 1, size);
}
return result;
}
}
object Main {
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
var text: String = "h o w to imp le ment th is alg orit hms";
text = obj.move_space(text);
print("After : [" + text + "]\n");
text = "this is simple text message";
text = obj.move_space(text);
print("After : [" + text + "]\n");
}
}
Output
Before : [h o w to imp le ment th is alg orit hms]
After : [howtoimplement thisalgorithms]
Before : [this is simple text message]
After : [thisissimpl etextmessage]
// Swift program
// Move all spaces at the middle of string
class MyString {
//Swapping two string elements by index
func swap(_ text: String, _ size: Int, _ a: Int, _ b: Int) -> String {
//Check valid location of swap element
if ((a >= 0 && a < size) && (b >= 0 && b < size)) {
var result = Array(text)
result.swapAt(a, b)
return String(result)
}
return text;
}
//Reverse the string elements of given size
func reverse(_ data: String, _ s: Int, _ e: Int, _ size: Int) -> String {
var start = s;
var last = e;
var result = data;
while (start < last) {
result = self.swap(result, size, start, last);
start += 1;
last -= 1;
}
return result;
}
func move_space(_ data: String) -> String {
var text = Array(data);
let size: Int = text.count;
var position: Int = 0;
var result: String = "";
print("Before : [\(data)]");
//Move non space character at the beginning position
var i: Int = 0;
while (i < size) {
if (text[i] != " ") {
result += String(text[i]);
position += 1;
}
i += 1;
}
if (position > 0) {
i = position;
while (i < size) {
result += " ";
i += 1;
}
//separate the string elements
result = self.reverse(result, position / 2, position - 1, size);
result = self.reverse(result, position / 2, size - 1, size);
}
return result;
}
}
func main() {
let obj: MyString = MyString();
var text: String = "h o w to imp le ment th is alg orit hms";
text = obj.move_space(text);
print("After : [\(text)]");
text = "this is simple text message";
text = obj.move_space(text);
print("After : [\(text)]");
}
main();
Output
Before : [h o w to imp le ment th is alg orit hms]
After : [howtoimplement thisalgorithms]
Before : [this is simple text message]
After : [thisissimpl etextmessage]
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment