Function to remove whitespace from string.

Discussion in 'C' started by sid78669, Sep 22, 2008.

  1. sid78669

    sid78669 New Member

    Joined:
    Sep 22, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hey, can anyone help me with this, i need to write a function which replace all tabs and new line character with spaces. remove all leading and trailing whitespaces. remove all but the first consecutive whitespace from the given string. any suggestions anybody??
     
  2. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    The first thing we tend to ask is:
    What do you have so far?
    Where are you stuck?
     
  3. sid78669

    sid78669 New Member

    Joined:
    Sep 22, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    lol! my bad. Well, this is what i have written so far, the function is a part of the whole program but can be used seperately as it just requires input of a string. The probelm so far is that it is now removing all spacces and is not ending the string at the require point, but just keeps whatever is left in the string, you have to compile it to better understand:

    Code:
    int cleanSpace( char s[])
    	{
    		int j = 0, i = 0, k = 0;        // 'j' is a local integers here which is used while clearing excess white space from the character array 's'.
    		
    		for(j = 0; !(s[j] == '\0'); j++)  //This loop converts the new line and tabs into
    		{                                          // spaces.
    			if (s[j] == '\t' || s[j] == '\n')
    			{
    				s[j] = ' ';
    			} 
    		}
    		
    		if (s[0] == ' ') //This condition searches for any leading whites pace.
    		{
    			k = 0;            // local variable used for help in the following for loop.
    			for (i = 0; i < strlen(s); i++) // converts spaces into following none space
    			{                                       // characters.
    				if (s[i] != ' ')
    				{
            				if (s[i] == '\0')
            				{
            					s[k] = '\0';
            				} else {
    						s[k] = s[i];
    					}
    					k++;                 //increase in value of k so as to move on to
    				}                              // the next character in the array.
    				
    			}
    			
    		}
    		
    		return strlen(s);  //I try to return the new string length for the given string. Not
    	}                               //Successful though!
    
    
    the function converts:
    " widget;Acme \t \n Co.;gear induction\tdevice:\v \n "
    into
    "widget;AcmeCo.;gearinductiondevice:\vctiondevice:\v "

    it is supposed to terminate the loop after the first \v in the new string, but keeps the previuos one going.
     
  4. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    Sorry I couldn't get back to you earlier. Busy day!
    However, your code makes my head hurt, so I'd rather suggest some pseudocode.
    Try something like this:
    Code:
    1. Find first non-whitespace char (call it's index "first")
    2. Find last non-whitespace char  (call it's index "last")
       Note: This requires that you search the string
             backwards from the end.
    3. Running index "i" from 0 to "last", including "last":
       a. Increment "first" past any whitespace you find
       b. If you found some whitespace, copy one space to
          index "i" and increment "i"
       c. str[i] = str[first]
       d. Increment both "i" and "first"
    4. Terminate string at index "i"
    Also note that the function isspace(str) returns true if the character is a space, tab,
    vertical tab, form feed, carriage return, or newline. You may find that useful!
     
  5. sid78669

    sid78669 New Member

    Joined:
    Sep 22, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Wow!! thats smart, let me get back to you with a code. Hope to see it works, but your logic is wonderful, pretty smart!!:hurray:
     
  6. sid78669

    sid78669 New Member

    Joined:
    Sep 22, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    ok, i tried to find the lead and trailing white space and copy the contents in between to a local string, 'temp' through the following code. If i could get it to work, i think i could have taken care of the white space in between, but alas! it does not work either!! here have a look:
    Code:
    int cleanSpace( char s[])
    	{
    		int i = 0, first = 0, last = 0; // 'i' - integer for count purpose, first is first non-whitespace character, last- last
    						// non-whitespace character.
    		char temp[250];			// temp string for saving string 's' without leading and trailing whitespace.
    		
    		for (i = 0; s[i+1] != ' ' ; i++) // for loop to find the first non-whitespace character and save its position as 'first'.
    		{
    			if(s[i] != ' ')
    			{
    				first = i;
    			}
    		}
    		
    		for (i = strlen(s); s[i - 1] != ' ' ; i--) // for loop to find the last non-whitespace character and save its position as
    		{					   //  'last'.
    			if(s[i] != ' ')
    			{
    				last = i;
    			}
    		}
    		
    		for (i = 0; i <= last ; i++)          // for loop to copy all characters in string 's' between 'first' & 'last' to local string
    		{				      // 'temp'.
    			if(i == last)
    			{
    				temp[i] = '\0';
    			} else {
    				temp[i] = s[first];
    				first++;
    			}
    		}
    		
                    strcpy(temp, s);
    		return strlen(temp);
    	}
    
     
  7. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    There was an error in my pseudocode.
    It says "i" should run from 0 to "last".
    Actually, "i" should start at 0, but the loop should continue while first <= last.
    Here's a rewrite of the code you have so far:
    Code:
    int cleanSpace( char s[])
    {
        int i = 0,      // general loop index
            first = 0,  // first non-whitespace character
            last = 0;   // last non-whitespace character
    
        // loop to find the first non-whitespace character
        // and save its position as 'first'.
        for (i = 0; s[i] != '\0'; i++)
        {
            if (!isspace(s[i]))
            {
                first = i;
                break;
            }
        }
    
        // loop to find the last non-whitespace character
        // and save its position as 'last'.
        for (i = strlen(s) - 1; i > first; i--)
        {
            if (!isspace(s[i]))
            {
                last = i;
                break;
            }
        }
    
        // for loop to copy all characters in string 's'
        // between 'first' & 'last' to local string 'temp'.
        for (i = 0; first <= last ; i++)
        {
            s[i] = s[first];
            first++;
        }
        s[i] = '\0';
    
        return strlen(s);
    }
     
  8. sid78669

    sid78669 New Member

    Joined:
    Sep 22, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    I cannot express how clean and easily understandable your code was. I liked it so much that i took it as my base code. I added a few more lines and now the function is working perfectly, taking out excess whitespace as well as new lines and horizontal tabs.
    Here have a look:
    Code:
    int cleanSpace( char s[])
            {
                    int i = 0, j = 0, // general loop index
                        first = 0,    // first non-whitespace character
                        last = 0;     // last non-whitespace character
    
                    // loop to find the first non-whitespace character
                    // and save its position as 'first'.
                    for (i = 0; s[i] != '\0'; i++)
                    {
                        if (s[i] != 32 && s[i] != 9 && s[i] != 10)
                        {
                            first = i;
                            break;
                        }
                    }
    
                    // loop to find the last non-whitespace character
                    // and save its position as 'last'.
                    for (i = strlen(s) - 1; i > first; i--)
                    {
                        if (s[i] != 32 && s[i] != 9 && s[i] != 10)
                        {
                            last = i;
                            break;
                        }
                    }
    
                    // for loop to copy all characters in string 's'
                    // between 'first' & 'last' to string 's'.
                    for (i = 0; first <= last ; i++)
                    {
                        s[i] = s[first];
                        first++;
                    }
                    s[i] = '\0';
    		
    		//for loop to find and replace white space characters
    		//with spaces.
    		for(i = 0; i < strlen(s) + 1; i++)
    		{
    			if (s[i] >= 9 && s[i] <= 10)
    			{
    				s[i] = 32;
    			}
    		}
    		
    		//for loop to remove excess whitespace if there are
    		//more than one consecutive whitespaces.
    		i = 0;
    		while (s[i] != '\0')
    		{
    			if(s[i] == 32 && s[i+1] == 32)
    			{
    				//Since excess white space was found, we shift
                                    //the whole string so as to take out the
    				//whitespace, regardless of what character is
    				//being copied.
    				for(j = i; s[j] != '\0'; j++)
    				{
             				s[j] = s[j+1];
    				}
    			} 
    			//If whitespace was not found, we increase index 'i' 
                           //and start looking at the next character.
    			else {
    				i++;
    			}
    		}
    
    		//Return the length of the "NEW" string 's'.
                    return strlen(s);
          }
    
    For everyone who might read, I give full credit to oogabooga for the code. I only added a few lines!! :D
     
  9. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    Good job! Just for your interest, here's how I would've done it.
    (Actually, I probably would've used pointers instead of indices.)
    The my_isspace function is similar to the built-in isspace function
    (in header ctype.h) except it doesn't include '\v', '\r', or '\f',
    which you don't seem to want (but you should check that out).
    Code:
    int my_isspace( char c )
    {
        return( c == ' ' || c == '\t' || c == '\n' );
    }
    
    int cleanSpace( char *s )
    {
        int i,           // general loop index
            first,       // index of first non-whitespace character
            last,        // index of last non-whitespace character
            found_space; // boolean: true if at least one whitespace was found
    
        // Find the first non-whitespace character.
        for( first = 0; s[first]; ++first )
            if( !my_isspace( s[first] ))
                break;
    
        // Find the last non-whitespace character.
        for( last = strlen(s) - 1; last > first; --last )
            if( !my_isspace( s[last] ))
                break;
    
        // Move characters in string 's' between 'first' & 'last' to the
        // beginning of string 's', converting one or more whitespace
        // characters to a single space.
        for( i = 0, found_space = 0; first <= last; ++i, ++first )
        {
            // While whitespace is found, skip past it.
            while( my_isspace( s[first] ))
            {
                ++first;
                found_space = 1;
            }
            // If whitespace was found, write one space to 's'
            if( found_space )
            {
                s[i++] = ' ';
                found_space = 0;
            }
            // Copy next character
            s[i] = s[first];
        }
        s[i] = '\0'; // Terminate the string
    
        // Return the length of the modified string 's'.
        return i;
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice