July 1, 2022

Given an string ,we have to make a program to remove space in string and print the result on screen.

Example:

Input:geeks prep
output:geeksprep

Logic:

step 1-we have to iterate string 
step 2-when if we get space then we skip it 
step 3-otherwise we add in new string.

code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    getline(cin,s);
    string k;
    for(int i=0;i<s.length();i++)
    {
        if(s[i]!=' ')
        {
            k+=s[i];
        }
        
    }
    cout<<k<<endl;
}

Output:

geeksprep

Time Complexity:O(N)

Space Complexity:O(N)

Leave a Reply

Your email address will not be published.