Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Getting Help is Easy!

Join 98,767 Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 1,025 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert

Register to Make This Box Go Away!



Deleting Extra Whitespaces in a String

This function removes extra spaces between a string.

Submitted By: born2c0de
Actions:
Rating:
Views: 955

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.


Add comment


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





Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->