Need help for a function

This is a discussion on Need help for a function within the C Programming forums, part of the General Programming Boards category; I need a fucntion that takes out all whitespace from a string as well as \n and \t. I'm a ...

  1. #1
    Unregistered
    Guest

    Post Need help for a function

    I need a fucntion that takes out all whitespace from a string as well as \n and \t.
    I'm a newbie.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Just loop through the entire array and copy anything that isn't whitespace.
    Code:
    int i, j = 0;
    char array[SIZE] = "  Now is the   time   ", 
         buffer[SIZE];
    
    for( i = 0; i < SIZE; ++i ){
      if( !isspace(array[i]) ){
        buffer[j] = array[i];
        j++;
      }
    }
    Last edited by Prelude; 01-24-2002 at 11:27 AM.
    My best code is written with the delete key.

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Won't that just make an identical copy of the original array?

    1st loop-
    i = 0
    array[0] = ' '
    so no copy...

    But on the 3rd loop
    i = 2
    array[2] = 'N'
    buffer[2] = 'N'

    See what I mean?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Not anymore, thanks for pointing that out Govt.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void RemoveWhiteSpace(char *p);
    
    int main(void)
    {
    	char str[] = "Remove\t\tall  white space\n and new lines";
    
    	RemoveWhiteSpace(str);
    
    	return 0;
    }
    
    void RemoveWhiteSpace(char *p)
    {
    	char *t;
    	int found = 0;
    
    	while (*p)
    	{
    		if (*p == ' ' || *p == '\t' || *p == '\n')
    		{
    			t = p;	// use temp pointer to move characters
    
    			while (*t)
    			{
    				*t = *(t + 1);
    				t++;
    			}
    
    			found = 1;
    		}
    		
    		if (!found)	// don't advance the pointer if we found a "filtered" character
    			p++;
    		else			
    			found = 0;
    	}
    }

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    I forgot to mention that my code will remove the whitespace from the *original* character array, where as Prelude's code will create a *new* character array.
    Last edited by clu82; 01-24-2002 at 11:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21