trim spaces in string c++

 Trim spaces in string c++

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
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';
}

SHARE

Milan Tomic

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.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment