Description
Get a string as input and then remove all the spaces present in the string.
Input
Hi Hello
Output
HiHello
C Program
#include<stdio.h>
#include<string.h>
int main()
{
char text[20], blank[20];
int c=0,d=0;
printf("Enter a string: ");
fgets(text,sizeof(text),stdin);
while(text[c]!='\0')
{
if(!(text[c] == ' '))
{
blank[d]=text[c];
d++;
}
c++;
}
blank[d]='\0';
printf("%s",blank);
return 0;
}
C++ Program
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char text[20], blank[20];
int c=0,d=0;
cout<<"Enter a string: ";
fgets(text,sizeof(text),stdin);
while(text[c]!='\0')
{
if(!(text[c] == ' '))
{
blank[d]=text[c];
d++;
}
c++;
}
blank[d]='\0';
cout<<blank;
return 0;
}
Java
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.print("Enter a string: ");
String str1 = sc.nextLine();
char[] a = str1.toCharArray();
StringBuffer str2 = new StringBuffer();
for (int i = 0; i < a.length; i++) {
if( (a[i] != ' ') && (a[i]!= '\t' )) {
str2.append(a[i]);
}
}
System.out.println("After removing space: "+str2);
}
}
Python
Str1 = input("Enter a string: ")
Str1 = "".join(Str1.split())
print("After removing spaces is: ",Str1)