Program Discussion :: Strings
50 / 60
Write a program to enter the string remove the blank spaces from the string.
Answer:
class RemoveSpace
{
public static void main(String[] args) {
String s1 ="java is a Programming language";
String s2=s1.replaceAll("\\s","");
System.out.println("Without Space String S2 is:" " " s2);
}
}
Asked In ::
Language:
Rakesh Mandal
18 May, 2017 8:04 AM
class RemoveSpace
{
public static void main(String[] args) {
String s1 ="java is a Programming language";
String s2=s1.replaceAll("\\s","");
System.out.println("Without Space String S2 is:" " " s2);
}
}
Language:
Varun Jain
15 Jun, 2017 10:57 PM
<!DOCTYPE html>
<html>
<body>
<?php
function Remove_space() {
$str ="what is your name"; // input string
echo "Input string is :" . $str."<br/>";
// conversion of string into array
$arr = explode( " ", $str);
$str = implode( "", $arr);
echo $str;
}
Remove_space();
?>
</body>
</html>
Language:
Latha
7 Jul, 2017 9:30 AM
#include <stdio.h>
int main()
{
char text[1000], blank[1000];
int c = 0, d = 0;
printf("Enter some text\n");
gets(text);
while (text[c] != '\0') {
if (text[c] == ' ') {
int temp = c + 1;
if (text[temp] != '\0') {
while (text[temp] == ' ' && text[temp] != '\0') {
if (text[temp] == ' ') {
c++;
}
temp++;
}
}
}
blank[d] = text[c];
c++;
d++;
}
blank[d] = '\0';
printf("Text after removing blanks\n%s\n", blank);
return 0;
}
Language:
Neel
7 Jul, 2017 9:30 AM
#include <iostream>
using namespace std;
char *removeSpaces(char *str)
{
int i = 0, j = 0;
while (str[i])
{
if (str[i] != ' ')
str[j++] = str[i];
i++;
}
str[j] = '\0';
return str;
}
/* Driver program to test above function*/
int main()
{
char str1[] = "gee k ";
cout << removeSpaces(str1) << endl;
char str2[] = " g e e k ";
cout << removeSpaces(str2);
return 0;
}
Language:
Amit
25 Aug, 2017 12:04 AM
class RemoveWhiteSpaces
{
public static void main(String[] args)
{
String str = " IBM Pro gram ms ";
//1. Using replaceAll() Method
String strWithoutSpace = str.replaceAll("\\s", "");
System.out.println(strWithoutSpace); //Output : IBMPrograms
//2. Without Using replaceAll() Method
char[] strArray = str.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < strArray.length; i )
{
if( (strArray[i] != ' ')&& (strArray[i] != '\t') )
{
sb.append(strArray[i]);
}
}
System.out.println(sb);
}
}
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.util.Scanner;
public class HelloWorld
{
public static void main(String []args)
{
String s="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string: ");
String str=sc.nextLine();
int len=str.length();
for(int i=0;i<len;i++)
{
char ch=str.charAt(i);
if(ch==' ')
continue;
else
s=s+ch;
}
System.out.println("The String after removing the blank spaces: "+s);
}
}