Remove spaces from a given string

Here given code implementation process.

// C Program
// Remove spaces from a given string
#include <stdio.h>

void remove_space(char str[],int size)
{
  int index=0;

  printf("\nBefore : %s\n",str );

  for (int i = 0; i < size; ++i)
  {
    if(str[i] != ' ')
    {
      // When not get space character 
      str[index++]=str[i];
    }
  }
  if(index < size)
  {
    // Set terminating character
    str[index] = '\0';
  }
}
int main()
{
  char str1[]="H i gher L ev  el Te st";

  //Get the size of array
  int size = sizeof(str1)/sizeof(str1[0])-1;
  
  remove_space(str1,size);
  
  printf("After : %s\n",str1 );

  char str2[]="Turn    Around";
  
  //Get the size of array
  size = sizeof(str2)/sizeof(str2[0])-1;
  
  remove_space(str2,size);
  
  printf("After : %s\n",str2 );

  return 0;
}

Output

Before : H i gher L ev  el Te st
After : HigherLevelTest

Before : Turn    Around
After : TurnAround
// Java program
// Remove spaces from a given string

public class MyString {
  
  public String remove_space(String str) {
    
    int size = str.length();

    int index = 0;

    String result ="";

    System.out.print("\nBefore : ["+str+"]\n");

    for (int i = 0; i < size; ++i) 
    {
      if (str.charAt(i) != ' ') 
      {
        // When not get space character 
        result += str.charAt(i);

        index++;
      }
    }
    return result;
  }
  public static void main(String[] args) 
  {

    MyString obj = new MyString();

    String text = "H i gher L ev  el Te st";

    text = obj.remove_space(text);
    
    System.out.print("After : ["+text+"]\n" );
    
    text ="Turn    Around";

    text = obj.remove_space(text);

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

  }
}

Output

Before : H i gher L ev  el Te st
After : [HigherLevelTest]

Before : Turn    Around
After : [TurnAround]
// C++ program
// Remove spaces from a given string
#include<iostream>

using namespace std;
class MyString {
	public:
		string remove_space(string str) {
			int size = str.size();
			int index = 0;
			string result = "";
			cout << "\nBefore : [" << str << "]\n";
			for (int i = 0; i < size; ++i) {
				if (str[i] != ' ') {
					// When not get space character 
					result += str[i];
					index++;
				}
			}
			return result;
		}
};
int main() {
	MyString obj = MyString();
	string text = "H i gher L ev  el Te st";
	text = obj.remove_space(text);
	cout << "After : [" << text << "]\n";
	text = "Turn    Around";
	text = obj.remove_space(text);
	cout << "After : [" << text << "]\n";
	return 0;
}

Output

Before : [H i gher L ev  el Te st]
After : [HigherLevelTest]

Before : [Turn    Around]
After : [TurnAround]
// C# program
// Remove spaces from a given string
using System;
public class MyString {
	public String remove_space(String str) {
		int size = str.Length;
		int index = 0;
		String result = "";
		Console.Write("\nBefore : [" + str + "]\n");
		for (int i = 0; i < size; ++i) {
			if (str[i] != ' ') {
				// When not get space character 
				result += str[i];
				index++;
			}
		}
		return result;
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		String text = "H i gher L ev  el Te st";
		text = obj.remove_space(text);
		Console.Write("After : [" + text + "]\n");
		text = "Turn    Around";
		text = obj.remove_space(text);
		Console.Write("After : [" + text + "]\n");
	}
}

Output

Before : [H i gher L ev  el Te st]
After : [HigherLevelTest]

Before : [Turn    Around]
After : [TurnAround]
<?php
// Php program
// Remove spaces from a given string
class MyString {
	public 	function remove_space($str) {
		$size = strlen($str);
		$index = 0;
		$result = "";
		echo("\nBefore : [". $str ."]\n");
		for ($i = 0; $i < $size; ++$i) {
			if ($str[$i] != ' ') {
				// When not get space character 
				$result .= $str[$i];
				$index++;
			}
		}
		return $result;
	}
}

function main() {
	$obj = new MyString();
	$text = "H i gher L ev  el Te st";
	$text = $obj->remove_space($text);
	echo("After : [". $text ."]\n");
	$text = "Turn    Around";
	$text = $obj->remove_space($text);
	echo("After : [". $text ."]\n");

}
main();

Output

Before : [H i gher L ev  el Te st]
After : [HigherLevelTest]

Before : [Turn    Around]
After : [TurnAround]
// Node Js program
// Remove spaces from a given string
class MyString {
	remove_space(str) {
		var size = str.length;
		var index = 0;
		var result = "";
		process.stdout.write("\nBefore : [" + str + "]\n");
		for (var i = 0; i < size; ++i) {
			if (str[i] != ' ') {
				// When not get space character 
				result += str[i];
				index++;
			}
		}

		return result;
	}
}

function main(args) {
	var obj = new MyString();
	var text = "H i gher L ev  el Te st";
	text = obj.remove_space(text);
	process.stdout.write("After : [" + text + "]\n");
	text = "Turn    Around";
	text = obj.remove_space(text);
	process.stdout.write("After : [" + text + "]\n");
}

main();

Output

Before : [H i gher L ev  el Te st]
After : [HigherLevelTest]

Before : [Turn    Around]
After : [TurnAround]
#  Python 3 program
#  Remove spaces from a given string
class MyString :
	def remove_space(self, str) :
		size = len(str)
		index = 0
		result = ""
		print("\nBefore : [{}]".format(str))
		i = 0
		while (i < size) :
			if (str[i] != ' ') :
				#  When not get space character 
				result += str[i]
				index += 1
			
			i += 1
		
		return result
	

def main() :
	obj = MyString()
	text = "H i gher L ev  el Te st"
	text = obj.remove_space(text)
	print("After : [{}]".format(text))
	text = "Turn    Around"
	text = obj.remove_space(text)
	print("After : [{}]".format(text))


if __name__ == "__main__":
	main()

Output

Before : [H i gher L ev  el Te st]
After : [HigherLevelTest]

Before : [Turn    Around]
After : [TurnAround]
#  Ruby program
#  Remove spaces from a given string
class MyString 
	def remove_space(str) 
		size = str.length()
		index = 0
		result = ""
		print("\nBefore  :[", str ,"]\n")
		i = 0
		while (i < size) 
			if (str[i] != ' ') 
				#  When not get space character 
				result += str[i]
				index += 1
			end
			i += 1
		end
		return result
	end
end
def main() 
	obj = MyString.new()
	text = "H i gher L ev  el Te st"
	text = obj.remove_space(text)
	print("After  :[", text ,"]\n")
	text = "Turn    Around"
	text = obj.remove_space(text)
	print("After  :[", text ,"]\n")
end
main()

Output

Before  :[H i gher L ev  el Te st]
After  :[HigherLevelTest]

Before  :[Turn    Around]
After  :[TurnAround]
// Scala program
// Remove spaces from a given string
class MyString {
	def remove_space(str: String): String = {
      
		var size: Int = str.length();
		var index: Int = 0;
		var result: String = "";
		print("\nBefore : [" + str + "]\n");
		var i: Int = 0;
		while (i < size) {
			if (str(i) != ' ') {
				// When not get space character 
				result += str(i);
				index += 1;
			}
			i += 1;
		}
		return result;
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		var text: String = "H i gher L ev  el Te st";
		text = obj.remove_space(text);
		print("After : [" + text + "]\n");
		text = "Turn    Around";
		text = obj.remove_space(text);
		print("After : [" + text + "]\n");
	}
}

Output

Before : [H i gher L ev  el Te st]
After : [HigherLevelTest]

Before : [Turn    Around]
After : [TurnAround]
// Swift program
// Remove spaces from a given string
class MyString {
	func remove_space(_ data: String) -> String {
      	var str = Array(data);
		let size: Int = str.count;
		var index: Int = 0;
		var result: String = "";print("\nBefore : [\(data)]");
		var i: Int = 0;
		while (i < size) {
			if (str[i] != " ") {
				// When not get space character 
				result += String(str[i]);
				index += 1;
			}
			i += 1;
		}
		return result;
	}
}
func main() {
	let obj: MyString = MyString();
	var text: String = "H i gher L ev  el Te st";
	text = obj.remove_space(text);
	print("After : [\(text)]");
	text = "Turn    Around";
	text = obj.remove_space(text);
	print("After : [\(text)]");
}
main();

Output

Before : [H i gher L ev  el Te st]
After : [HigherLevelTest]

Before : [Turn    Around]
After : [TurnAround]


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