Trim spaces in string c++
There can be multiple spaces present after any word.
String S
Updated string
Constraints :
1 <= Length of string S <= 10^6
Sample Input :
abc def g hi
Sample Output :
abcdefghi
c++ code :
void trimSpaces(char input[]) {
/* Don't write main().
* Don't read input, it is passed as function argument.
* Don't print or return the output.
* Change in the given input string itself.
* Taking input and printing output is handled automatically.*/
if(input==nullptr) return;
int len=0;
while(input[len]!='\0')
len++;
int n=0;
for(int i=0;i<len;i++)
{
if(input[i]!=' ')
{
input[n]=input[i];
n++;
}
}
input[n]='\0';
}
Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.
0 comments:
Post a Comment