Trim Spaces

 

Given an input string S that contains multiple words, you need to remove all the spaces present in the input string.

There can be multiple spaces present after any word.

Input Format :
 String S
Output Format :
Updated string
Constraints :

1 <= Length of string S <= 10^6

Sample Input :
abc def g hi
Sample Output :
abcdefghi



#include <iostream>

#include <cstring>

using namespace std;

int lenght(char str[]){

    int count=0;

    for (int i=0;str[i]!='\0';i++){

        count++;

    }

    return count;

}


void trimSpaces(char input[]) {

    // Write your code here

    for(int i=0;i<lenght(input);i++){

        if(input[i]== ' '){

            for(int j=i;j<lenght(input);j++){

                input[j]=input[j+1];

            }

            

            

        }

    }

}

#include "solution.h"


int main() {

    char input[1000000];

    cin.getline(input, 1000000);

    trimSpaces(input);

    cout << input << endl;

}

///////////////////////////////////////////////////////////////
int lenght(char str[]){
    int count=0;
    for (int i=0;str[i]!='\0';i++){
        count++;
    }
    return count;
}

void trimSpaces(char str[]) {
    // Write your code here
    int i = 0, j = 0; 
    while (str[i]) 
    { 
        if (str[i] != ' ') 
           str[j++] = str[i]; 
        i++; 
    } 
    str[j] = '\0'; 
     
}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings