Move all spaces at beginning of string

Here given code implementation process.

//C Program 
//Move all spaces at beginning of string
#include <stdio.h>

void move_space(char str[],int size)
{ 
  
  int position = size-1;
  printf("Before : [%s]\n",str );
  for (int i = size-1; i >= 0; --i)
  {

    if(str[i]!=' ')
    {
      //When get a space
      str[position]=str[i];
      position--;
    }
  }
  for (int i = 0; i <= position; ++i)
  {
    str[i]=' ';
  }
}
int main()
{


  char str1[]=" text c  o  o  l set word";

  int s1=sizeof(str1)/sizeof(str1[0])-1;
 
  move_space(str1,s1);
  
  printf("After  : [%s]\n",str1 );



  char str2[]="I a  m beg   inning  ";

  s1=sizeof(str2)/sizeof(str2[0])-1;
 
  move_space(str2,s1);
  
  printf("After  : [%s]\n",str2 );

  return 0;
}

Output

Before : [ text c  o  o  l set word]
After  : [          textcoolsetword]
Before : [I a  m beg   inning  ]
After  : [         Iambeginning]
// Java program
// Move all spaces at beginning of string

public class MyString {

  public String move_space(String str) 
  {
    int size = str.length();

    int position = 0;

    String result="";
    System.out.print("Before : ["+str+"]\n");
    for (int i = 0; i <size; ++i) 
    {
      if (str.charAt(i) != ' ') 
      {
        //When get a space
        result += str.charAt(i);

        position++;
      }
    }
    for (int i = position; i < size; ++i) {
      result = ' '+result;
    }
    return result;
  }
  public static void main(String[] args) 
  {

    MyString obj = new MyString();
    
    String text = " text c  o  o  l set word";
    
    text =  obj.move_space(text);

    System.out.print("After  : ["+text+"]\n");


    text = "I a  m beg   inning  ";
    
    text =  obj.move_space(text);

    System.out.print("After  : ["+text+"]\n");
  
  }
}

Output

Before : [ text c  o  o  l set word]
After  : [          textcoolsetword]
Before : [I a  m beg   inning  ]
After  : [         Iambeginning]
// C++ program
// Move all spaces at beginning of string
#include<iostream>

using namespace std;
class MyString {
	public:
		string move_space(string str) {
			int size = str.size();
			int position = 0;
			string result = "";
			cout << "Before : [" << str << "]\n";
			for (int i = 0; i < size; ++i) {
				if (str[i] != ' ') {
					//When get a space
					result += str[i];
					position++;
				}
			}
			for (int i = position; i < size; ++i) {
				result = ' ' + result;
			}
			return result;
		}
};
int main() {
	MyString obj =  MyString();
	string text = " text c o o l set word";
	text = obj.move_space(text);
	cout << "After  : [" << text << "]\n";
	text = "I a m beg inning ";
	text = obj.move_space(text);
	cout << "After  : [" << text << "]\n";
	return 0;
}

Output

Before : [ text c o o l set word]
After  : [       textcoolsetword]
Before : [I a m beg inning ]
After  : [     Iambeginning]
// C# program
// Move all spaces at beginning of string
using System;
public class MyString {
	public String move_space(String str) {
		int size = str.Length;
		int position = 0;
		String result = "";
		Console.Write("Before : [" + str + "]\n");
		for (int i = 0; i < size; ++i) {
			if (str[i] != ' ') {
				//When get a space
				result += str[i];
				position++;
			}
		}
		for (int i = position; i < size; ++i) {
			result = ' ' + result;
		}
		return result;
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		String text = " text c o o l set word";
		text = obj.move_space(text);
		Console.Write("After  : [" + text + "]\n");
		text = "I a m beg inning ";
		text = obj.move_space(text);
		Console.Write("After  : [" + text + "]\n");
	}
}

Output

Before : [ text c o o l set word]
After  : [       textcoolsetword]
Before : [I a m beg inning ]
After  : [     Iambeginning]
<?php
// Php program
// Move all spaces at beginning of string
class MyString {
	public 	function move_space($str) {
		$size = strlen($str);
		$position = 0;
		$result = "";
		echo("Before : [". $str ."]\n");
		for ($i = 0; $i < $size; ++$i) {
			if ($str[$i] != ' ') {
				//When get a space
				$result .= $str[$i];
				$position++;
			}
		}
		for ($i = $position; $i < $size; ++$i) {
			$result = ' ' . $result;
		}
		return $result;
	}
}

function main() {
	$obj = new MyString();
	$text = " text c o o l set word";
	$text = $obj->move_space($text);
	echo("After  : [". $text ."]\n");
	$text = "I a m beg inning ";
	$text = $obj->move_space($text);
	echo("After  : [". $text ."]\n");

}
main();

Output

Before : [ text c o o l set word]
After  : [       textcoolsetword]
Before : [I a m beg inning ]
After  : [     Iambeginning]
// Node Js program
// Move all spaces at beginning of string
class MyString {
	move_space(str) {
		var size = str.length;
		var position = 0;
		var result = "";
		process.stdout.write("Before : [" + str + "]\n");
		for (var i = 0; i < size; ++i) {
			if (str[i] != ' ') {
				//When get a space
				result += str[i];
				position++;
			}
		}

		for (var i = position; i < size; ++i) {
			result = ' ' + result;
		}

		return result;
	}
}

function main(args) {
	var obj = new MyString();
	var text = " text c o o l set word";
	text = obj.move_space(text);
	process.stdout.write("After  : [" + text + "]\n");
	text = "I a m beg inning ";
	text = obj.move_space(text);
	process.stdout.write("After  : [" + text + "]\n");
}

main();

Output

Before : [ text c o o l set word]
After  : [       textcoolsetword]
Before : [I a m beg inning ]
After  : [     Iambeginning]
#  Python 3 program
#  Move all spaces at beginning of string
class MyString :
	def move_space(self, str) :
		size = len(str)
		position = 0
		result = ""
		print("Before : [{}]".format(str))
		i = 0
		while (i < size) :
			if (str[i] != ' ') :
				# When get a space
				result += str[i]
				position += 1
			
			i += 1
		
		i = position
		while (i < size) :
			result = ' ' + result
			i += 1
		
		return result
	

def main() :
	obj = MyString()
	text = " text c o o l set word"
	text = obj.move_space(text)
	print("After  : [{}]".format(text))
	text = "I a m beg inning "
	text = obj.move_space(text)
	print("After  : [{}]".format(text))


if __name__ == "__main__":
	main()

Output

Before : [ text c o o l set word]
After  : [       textcoolsetword]
Before : [I a m beg inning ]
After  : [     Iambeginning]
#  Ruby program
#  Move all spaces at beginning of string
class MyString 
	def move_space(str) 
		size = str.length()
		position = 0
		result = ""
		print("Before  :[", str ,"]\n")
		i = 0
		while (i < size) 
			if (str[i] != ' ') 
				# When get a space
				result += str[i]
				position += 1
			end
			i += 1
		end
		i = position
		while (i < size) 
			result = ' ' + result
			i += 1
		end
		return result
	end
end
def main() 
	obj = MyString.new()
	text = " text c o o l set word"
	text = obj.move_space(text)
	print("After  : [", text ,"]\n")
	text = "I a m beg inning "
	text = obj.move_space(text)
	print("After  : [", text ,"]\n")
end
main()

Output

Before  :[ text c o o l set word]
After  : [       textcoolsetword]
Before  :[I a m beg inning ]
After  : [     Iambeginning]
// Scala program
// Move all spaces at beginning of string
class MyString {
	def move_space(str: String): String = {
		var size: Int = str.length();
		var position: Int = 0;
		var result: String = "";
		print("Before : [" + str + "]\n");
		var i: Int = 0;
		while (i < size) {
			if (str(i) != ' ') {
				//When get a space
				result += str(i);
				position += 1;
			}
			i += 1;
		}
		i = position;
		while (i < size) {
			result = " " + result;
			i += 1;
		}
		return result;
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		var text: String = " text c o o l set word";
		text = obj.move_space(text);
		print("After  : [" + text + "]\n");
		text = "I a m beg inning ";
		text = obj.move_space(text);
		print("After  : [" + text + "]\n");
	}
}

Output

Before : [ text c o o l set word]
After  : [       textcoolsetword]
Before : [I a m beg inning ]
After  : [     Iambeginning]
// Swift program
// Move all spaces at beginning of string
class MyString {
	func move_space(_ text: String) -> String {
      	var str = Array(text);
		let size: Int = str.count;
		var position: Int = 0;
		var result: String = "";
      	
      	print("Before : [", text ,"]");
		var i: Int = 0;
		while (i < size) {
			if (str[i] != " ") {
				//When get a space
				result += String(str[i]);
				position += 1;
			}
			i += 1;
		}
		i = position;
		while (i < size) {
			result = " " + result;
			i += 1;
		}
		return result;
	}
}
func main() {
	let obj: MyString = MyString();
	var text: String = " text c o o l set word";
	text = obj.move_space(text);
	print("After  : [\(text)]");
	text = "I a m beg inning ";
	text = obj.move_space(text);
	print("After  : [\(text)]");
}
main();

Output

Before : [  text c o o l set word ]
After  : [       textcoolsetword]
Before : [ I a m beg inning  ]
After  : [     Iambeginning]


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







© 2021, kalkicode.com, All rights reserved