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;
}
---------------------------------------------------------------------------------
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
Post a Comment