Remove spaces from a given string

PROBLEM :

Given a string, remove spaces from it

Input: First line of the input is the number of test cases T. And first line of test case contains a string whose spaces are to be removed.

Output:  Modified string without any space

Constraints:  Input String Length <= 1000

Example:
Input:
2
geeks  for geeks
    g f g

Output:
geeksforgeeks
gfg

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<string.h>
int main()
 {
int t,i,l,j ;
char str[1000] ;
cin>>t ;
while(t--)
{
   scanf(" %[^\n]s",str) ;
 
   l=strlen(str) ;
   j=0 ;
   for(i=0;i<l;i++)
   {
       if(str[i]!=' ')
       {
           str[j]=str[i] ;
           j++ ;
       }
   }
   str[j]='\0' ;
   cout<<str<<endl ;
}
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )