Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 244,263 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,247 people online right now. Registration is fast and FREE... Join Now!





Deleting Extra Whitespaces in a String

This function removes extra spaces between a string.

Submitted By: born2c0de
Actions:
Rating:
Views: 2,128

Language: C

Last Modified: October 10, 2007
Instructions: This Code removes only those extra whitespaces which are present between the string and not those which are present in the beginning or at the end of the string.

Snippet


  1. #include <string.h>
  2.  
  3. /* The Modifications are Made to the Same String */
  4. void inside_trim(char* x)
  5. {
  6.         unsigned int i,pos,nxtchar;
  7.  
  8.         for(i=0;i<strlen(x);i++)
  9.         {
  10.                 if( x[i] == ' ' )
  11.                 {
  12.                         pos=i+1;
  13.                         nxtchar=pos-1;
  14.                         while(x[nxtchar++] == ' '){}
  15.  
  16.                         strcpy(&x[pos],&x[nxtchar-1]);
  17.                 }
  18.         }
  19. }
  20.  
  21. /* USAGE:
  22.         char xyz[]="Does      this     work?";
  23.         printf("\n%s",xyz);
  24.         inside_trim(&xyz[0]);
  25.         printf("\n%s",xyz);
  26. */

Copy & Paste


Comments


thakur1984 2008-07-07 05:57:06

this code is not working. there is some amendments required in this code.there should be strcpy(&x[nxtchar],&x[pos]); there is no need of while loop.

David W 2008-11-07 22:32:26

try this ... void stripLeadingSpaces( char str[] ) { int i, j=0; while (str[j]==' ') ++j; for (i=0; str[j]!=0; ++i, ++j) str=str[j]; str=0; }

David W 2008-11-07 22:34:39

David W 2008-11-07 22:38:32

For some strange reason the [ i ] was NOT there above ... it should be ... str[ i ] = str[ j ]; str[ i ] = 0; }


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month