TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Remove All the White Spaces from a String

Program to Remove All the White Spaces from a String on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Q. Program to remove all the white spaces from a string.

In this program, our task is to remove all the white-spaces from the string. For this purpose, we need to traverse the string and check if any character of the string is matched with a white-space character or not. If so, Use any built-in method like replace() with a blank.

In C, we do not have any built-in method to replace. Therefore, We need to run the for loop to traverse the string and see if there is any white-space character or not. If so, then start the inner loop (j) from ith character to len and keep replacing each element with its next adjacent element. Decrease the length of the string by 1 on the termination of this loop. Repeat this process until all the white-spaces of the string are removed.

Algorithm

  1. Define a string.
  2. Use replace () function to replace all the space characters with a blank.
  3. The resulting string will not have any spaces in-between them.

Solution

Python

str1 = "Remove white spaces";
 
#Using in-built function in python
#Here, we are replacing space character with blank
str1 = str1.replace(" ","");
        
print("String after removing all the white spaces : " + str1);

Output:

String after removing all the white spaces : Removewhitespaces

C

#include <stdio.h>
int main()
{
    int i, len = 0,j;
    char str[] = "Remove white spaces";
    
    //Calculating length of the array
    len = sizeof(str)/sizeof(str[0]);
    
    //Checks for space character in array if its there then ignores it and swap str[i] to str[i+1];
    for(i = 0; i < len; i++){
        if(str[i] == ' '){
            for(j=i;j<len;j++)
		{
			str[j]=str[j+1];
		}
		len--;
       	}
	}
    printf("String after removing all the white spaces : %s", str);
    return 0;
}

Output:

String after removing all the white spaces : Removewhitespaces

JAVA

public class removeWhiteSpace {
    public static void main(String[] args) {
        
        String str1="Remove white spaces";
        
        //Removes the white spaces using regex
        str1 = str1.replaceAll("\\s+", "");
        
        System.out.println("String after removing all the white spaces : " + str1);
    }
}

Output:

String after removing all the white spaces : Removewhitespaces

C#

using System;
public class Program
{
    public static void Main()
    {
        string str1="Remove white spaces";
        
        //Removes the white spaces using regex
        str1 = str1.Replace(" ",String.Empty);
        
        Console.WriteLine("String after removing all the white spaces : " + str1);
    }
}

Output:

String after removing all the white spaces : Removewhitespaces

PHP

<!DOCTYPE html>
<html>
<body>
<?php
    $str1 = "Remove white spaces";
    //Replaces every space character with blank
    $str1 = str_replace(' ','',$str1);
    echo "String after removing all the white spaces : $str1";
?>
</body>
</html>
 
 

Output:

String after removing all the white spaces : Removewhitespaces

Next TopicPrograms List




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf