Program to Delete White Spaces From the String
- Write a program to Delete White Spaces From the String in C
- Write a program to Delete White Spaces From the String in C++
- Write a program to Delete White Spaces From the String in Python
- Write a program to Delete White Spaces From the String in PHP
- Write a program to Delete White Spaces From the String in Java
- Write a program to Delete White Spaces From the String in Java Script
- Write a program to Delete White Spaces From the String in C#
Explanation:
When you delete white spaces from a string, all of the space characters—such as tabs, spaces, and so on—are eliminated, leaving only the non-whitespace letters.
Algorithm:
- Start with an empty result string.
- Loop through each character of the input string.
- If the character is not a whitespace (‘ ‘), append it to the result.
- Return the final result string without white spaces.
Program to Delete White Spaces From the String
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> #include <string.h> void removeSpaces(char *str) { int i = 0, j = 0; while (str[i]) { if (str[i] != ' ') str[j++] = str[i]; i++; } str[j] = '\0'; } int main() { char str[100]; // Input string printf("Enter a string: "); fgets(str, sizeof(str), stdin); str[strcspn(str, "\n")] = '\0'; // Remove newline character // Remove white spaces removeSpaces(str); printf("String after removing white spaces: %s\n", str); return 0; }
#include <iostream> #include <string> void removeSpaces(std::string &str) { str.erase(remove(str.begin(), str.end(), ' '), str.end()); } int main() { std::string str; // Input string std::cout << "Enter a string: "; std::getline(std::cin, str); // Remove white spaces removeSpaces(str); std::cout << "String after removing white spaces: " << str << std::endl; return 0; }
str1 = input("Enter a string: ") # Remove white spaces result = str1.replace(" ", "") # Output result print(f"String after removing white spaces: {result}")
<?php // Input string echo "Enter a string: "; $str = trim(fgets(STDIN)); // Remove white spaces $result = str_replace(" ", "", $str); // Output result echo "String after removing white spaces: $result\n"; ?>
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input string System.out.print("Enter a string: "); String str = scanner.nextLine(); // Remove white spaces String result = str.replaceAll(" ", ""); // Output result System.out.println("String after removing white spaces: " + result); } }
let str = prompt("Enter a string: "); // Remove white spaces let result = str.replace(/ /g, ""); // Output result console.log("String after removing white spaces: " + result);
using System; class Program { static void Main() { // Input string Console.Write("Enter a string: "); string str = Console.ReadLine(); // Remove white spaces string result = str.Replace(" ", ""); // Output result Console.WriteLine("String after removing white spaces: " + result); } }