Program to Remove spaces from a string

Program to Remove spaces from a string

21 April 2023

21 April 2023

Write a Program to Remove spaces from a string


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

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)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023


Related Articles

Ask Us Anything !