Posted on by Kalkicode
Code String

Remove extra spaces from a string

Here given code implementation process.

// C program
// Remove extra spaces from a string
#include <stdio.h>

void remove_extra_space(char str[],int size)
{
  int index=0;
  printf("Before : [%s]\n",str );
  for (int i = 0; i < size; ++i)
  {
    if(str[i]!=' ')
    {
      if(index > 0 && i > 0 && str[index-1] != ' ' && str[i-1]==' ')
      {
        //include space between 2 words
        str[index]=' ';
        index++;
      }
      //include new character
      str[index] = str[i];
      index++;
    }
  }
  if(index < size)
  {
    //Add terminating character
    str[index]='\0';
  }
}
int main()
{

  //Test cases

  char str1[]="    This is    simple text string  ";

  int size = sizeof(str1)/sizeof(str1[0])-1;

  remove_extra_space(str1,size);

  printf("After : [%s]\n\n",str1 );


  char str2[]="nice       code";

  size = sizeof(str2)/sizeof(str2[0])-1;

  remove_extra_space(str2,size);

  printf("After : [%s]\n\n",str2 );


  char str3[]="          ";

  size = sizeof(str3)/sizeof(str3[0])-1;

  remove_extra_space(str3,size);

  printf("After : [%s]\n\n",str3 );


  char str4[]="    happy      ";

  size = sizeof(str4)/sizeof(str4[0])-1;

  remove_extra_space(str4,size);

  printf("After : [%s]\n\n",str4 );

  char str5[]="simple      ";

  size = sizeof(str5)/sizeof(str5[0])-1;

  remove_extra_space(str5,size);

  printf("After : [%s]\n\n",str5 );

  return 0;
}

Output

Before : [    This is    simple text string  ]
After : [This is simple text string]

Before : [nice       code]
After : [nice code]

Before : [          ]
After : []

Before : [    happy      ]
After : [happy]

Before : [simple      ]
After : [simple]
// C++ program
// Remove extra spaces from a string
#include<iostream>

using namespace std;
class MyString {
	public:
		string remove_extra_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] != ' ') {
					if (index > 0 &&
						i > 0 &&
						str[index - 1] != ' ' &&
						str[i - 1] == ' ') {
						//include space between 2 words
						result += " ";
						index++;
					}
					//include new character
					result += str[i];
					index++;
				}
			}
			return result;
		}
};
int main() {
	MyString obj =  MyString();
	string text = "    This is    simple text string  ";
	text = obj.remove_extra_space(text);
	cout << "After : [" << text << "] \n";
	// When have intermediate space
	text = "nice       code";
	text = obj.remove_extra_space(text);
	cout << "After : [" << text << "] \n";
	// When string containing all empty spaces
	text = "          ";
	text = obj.remove_extra_space(text);
	cout << "After : [" << text << "] \n";
	//When empty spaces are exist front and last position
	text = "    happy      ";
	text = obj.remove_extra_space(text);
	cout << "After : [" << text << "] \n";
	//When empty spaces are exist at last position
	text = "simple      ";
	text = obj.remove_extra_space(text);
	cout << "After : [" << text << "] \n";
	return 0;
}

Output

Before :[    This is    simple text string  ]
After : [Thisis simpletext string]

Before :[nice       code]
After : [nice code]

Before :[          ]
After : []

Before :[    happy      ]
After : [happy]

Before :[simple      ]
After : [simple]
// C# program
// Remove extra spaces from a string
using System;
public class MyString {
	public String remove_extra_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] != ' ') {
				if (index > 0 &&
					i > 0 &&
					str[index - 1] != ' ' &&
					str[i - 1] == ' ') {
					//include space between 2 words
					result += " ";
					index++;
				}
				//include new character
				result += str[i];
				index++;
			}
		}
		return result;
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		String text = "    This is    simple text string  ";
		text = obj.remove_extra_space(text);
		Console.Write("After : [" + text + "] \n");
		// When have intermediate space
		text = "nice       code";
		text = obj.remove_extra_space(text);
		Console.Write("After : [" + text + "] \n");
		// When string containing all empty spaces
		text = "          ";
		text = obj.remove_extra_space(text);
		Console.Write("After : [" + text + "] \n");
		//When empty spaces are exist front and last position
		text = "    happy      ";
		text = obj.remove_extra_space(text);
		Console.Write("After : [" + text + "] \n");
		//When empty spaces are exist at last position
		text = "simple      ";
		text = obj.remove_extra_space(text);
		Console.Write("After : [" + text + "] \n");
	}
}

Output

Before :[    This is    simple text string  ]
After : [Thisis simpletext string]

Before :[nice       code]
After : [nice code]

Before :[          ]
After : []

Before :[    happy      ]
After : [happy]

Before :[simple      ]
After : [simple]
<?php
// Php program
// Remove extra spaces from a string
class MyString {
	public 	function remove_extra_space($str) {
		$size = strlen($str);
		$index = 0;
		$result = "";
		echo("\nBefore :[". $str ."]\n");
		for ($i = 0; $i < $size; ++$i) {
			if ($str[$i] != ' ') {
				if ($index > 0 &&
					$i > 0 &&
					$str[$index - 1] != ' ' &&
					$str[$i - 1] == ' ') {
					//include space between 2 words
					$result .= " ";
					$index++;
				}
				//include new character
				$result .= $str[$i];
				$index++;
			}
		}
		return $result;
	}
}

function main() {
	$obj = new MyString();
	$text = "    This is    simple text string  ";
	$text = $obj->remove_extra_space($text);
	echo("After : [". $text ."] \n");
	// When have intermediate space
	$text = "nice       code";
	$text = $obj->remove_extra_space($text);
	echo("After : [". $text ."] \n");
	// When string containing all empty spaces
	$text = "          ";
	$text = $obj->remove_extra_space($text);
	echo("After : [". $text ."] \n");
	//When empty spaces are exist front and last position
	$text = "    happy      ";
	$text = $obj->remove_extra_space($text);
	echo("After : [". $text ."] \n");
	//When empty spaces are exist at last position
	$text = "simple      ";
	$text = $obj->remove_extra_space($text);
	echo("After : [". $text ."] \n");

}
main();

Output

Before :[    This is    simple text string  ]
After : [Thisis simpletext string]

Before :[nice       code]
After : [nice code]

Before :[          ]
After : []

Before :[    happy      ]
After : [happy]

Before :[simple      ]
After : [simple]
// Node Js program
// Remove extra spaces from a string
class MyString {
	remove_extra_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] != ' ') {
				if (index > 0 &&
					i > 0 &&
					str[index - 1] != ' ' &&
					str[i - 1] == ' ') {
					//include space between 2 words
					result += " ";
					index++;
				}

				//include new character
				result += str[i];
				index++;
			}
		}

		return result;
	}
}

function main(args) {
	var obj = new MyString();
	var text = "    This is    simple text string  ";
	text = obj.remove_extra_space(text);
	process.stdout.write("After : [" + text + "] \n");
	// When have intermediate space
	text = "nice       code";
	text = obj.remove_extra_space(text);
	process.stdout.write("After : [" + text + "] \n");
	// When string containing all empty spaces
	text = "          ";
	text = obj.remove_extra_space(text);
	process.stdout.write("After : [" + text + "] \n");
	//When empty spaces are exist front and last position
	text = "    happy      ";
	text = obj.remove_extra_space(text);
	process.stdout.write("After : [" + text + "] \n");
	//When empty spaces are exist at last position
	text = "simple      ";
	text = obj.remove_extra_space(text);
	process.stdout.write("After : [" + text + "] \n");
}

main();

Output

Before :[    This is    simple text string  ]
After : [Thisis simpletext string]

Before :[nice       code]
After : [nice code]

Before :[          ]
After : []

Before :[    happy      ]
After : [happy]

Before :[simple      ]
After : [simple]
#  Python 3 program
#  Remove extra spaces from a string
class MyString :
	def remove_extra_space(self, str) :
		size = len(str)
		index = 0
		result = ""
		print("\nBefore: [{}] ".format(str))
		i = 0
		while (i < size) :
			if (str[i] != ' ') :
				if (index > 0 and i > 0 and str[index - 1] != ' '
					and str[i - 1] == ' ') :
					# include space between 2 words
					result += " "
					index += 1
				
				# include new character
				result += str[i]
				index += 1
			
			i += 1
		
		return result
	

def main() :
	obj = MyString()
	text = "    This is    simple text string  "
	text = obj.remove_extra_space(text)
	print("After : [{}] ".format(text))
	#  When have intermediate space
	text = "nice       code"
	text = obj.remove_extra_space(text)
	print("After : [{}] ".format(text))
	#  When string containing all empty spaces
	text = "          "
	text = obj.remove_extra_space(text)
	print("After : [{}] ".format(text))
	# When empty spaces are exist front and last position
	text = "    happy      "
	text = obj.remove_extra_space(text)
	print("After : [{}] ".format(text))
	# When empty spaces are exist at last position
	text = "simple      "
	text = obj.remove_extra_space(text)
	print("After : [{}] ".format(text))


if __name__ == "__main__":
	main()

Output

Before: [    This is    simple text string  ]
After : [Thisis simpletext string]

Before: [nice       code]
After : [nice code]

Before: [          ]
After : []

Before: [    happy      ]
After : [happy]

Before: [simple      ]
After : [simple]
#  Ruby program
#  Remove extra spaces from a string
class MyString 
	def remove_extra_space(str) 
		size = str.length()
		index = 0
		result = ""
		print("\nBefore  :[", str ,"]\n")
		i = 0
		while (i < size) 
			if (str[i] != ' ') 
				if (index > 0 &&
					i > 0 &&
					str[index - 1] != ' ' &&
					str[i - 1] == ' ') 
					# include space between 2 words
					result += " "
					index += 1
				end
				# include new character
				result += str[i]
				index += 1
			end
			i += 1
		end
		return result
	end
end
def main() 
	obj = MyString.new()
	text = "    This is    simple text string  "
	text = obj.remove_extra_space(text)
	print("After  :[", text ,"] \n")
	#  When have intermediate space
	text = "nice       code"
	text = obj.remove_extra_space(text)
	print("After  :[", text ,"] \n")
	#  When string containing all empty spaces
	text = "          "
	text = obj.remove_extra_space(text)
	print("After  :[", text ,"] \n")
	# When empty spaces are exist front and last position
	text = "    happy      "
	text = obj.remove_extra_space(text)
	print("After  :[", text ,"] \n")
	# When empty spaces are exist at last position
	text = "simple      "
	text = obj.remove_extra_space(text)
	print("After  :[", text ,"] \n")
end
main()

Output

Before  :[    This is    simple text string  ]
After  :[Thisis simpletext string] 

Before  :[nice       code]
After  :[nice code] 

Before  :[          ]
After  :[] 

Before  :[    happy      ]
After  :[happy] 

Before  :[simple      ]
After  :[simple] 
// Scala program
// Remove extra spaces from a string
class MyString {
	def remove_extra_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) != ' ') {
				if (index > 0 &&
					i > 0 &&
					str(index - 1) != ' ' &&
					str(i - 1) == ' ') {
					//include space between 2 words
					result += " ";
					index += 1;
				}
				//include new 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 = "    This is    simple text string  ";
		text = obj.remove_extra_space(text);
		print("After : [" + text + "] \n");

		// When have intermediate space
		text = "nice       code";
		text = obj.remove_extra_space(text);
		print("After : [" + text + "] \n");

		// When string containing all empty spaces
		text = "          ";
		text = obj.remove_extra_space(text);
		print("After : [" + text + "] \n");

		//When empty spaces are exist front and last position
		text = "    happy      ";
		text = obj.remove_extra_space(text);
		print("After : [" + text + "] \n");

		//When empty spaces are exist at last position
		text = "simple      ";
		text = obj.remove_extra_space(text);
		print("After : [" + text + "] \n");
	}
}

Output

Before :[    This is    simple text string  ]
After : [Thisis simpletext string]

Before :[nice       code]
After : [nice code]

Before :[          ]
After : []

Before :[    happy      ]
After : [happy]

Before :[simple      ]
After : [simple]
// Swift program
// Remove extra spaces from a string
class MyString {
	func remove_extra_space(_ text: String) -> String {
        var str = Array(text);
		let size: Int = str.count;
		var index: Int = 0;
		var result: String = "";print("\nBefore :[\(text)] ");
		var i: Int = 0;
		while (i < size) {
			if (str[i] != " ") {
				if (index > 0 &&
					i > 0 &&
					str[index - 1] != " " &&
					str[i - 1] == " ") {
					//include space between 2 words
					result += " ";
					index += 1;
				}
				//include new character
				result += String(str[i]);
				index += 1;
			}
			i += 1;
		}
		return result;
	}
}
func main() {
	let obj: MyString = MyString();
	var text: String = "    This is    simple text string  ";
	text = obj.remove_extra_space(text);
	print("After : [\(text)] ");
	// When have intermediate space
	text = "nice       code";
	text = obj.remove_extra_space(text);
	print("After : [\(text)] ");
	// When string containing all empty spaces
	text = "          ";
	text = obj.remove_extra_space(text);
	print("After : [\(text)] ");
	//When empty spaces are exist front and last position
	text = "    happy      ";
	text = obj.remove_extra_space(text);
	print("After : [\(text)] ");
	//When empty spaces are exist at last position
	text = "simple      ";
	text = obj.remove_extra_space(text);
	print("After : [\(text)] ");
}
main();

Output

Before :[    This is    simple text string  ]
After : [Thisis simpletext string]

Before :[nice       code]
After : [nice code]

Before :[          ]
After : []

Before :[    happy      ]
After : [happy]

Before :[simple      ]
After : [simple]

Comment

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