Saturday, December 4, 2021
HomeTop Coding QuestionWrite a program to remove spaces in string.

Write a program to remove spaces in string.

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

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments