Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Function to remove whitespace from string. (http://www.go4expert.com/forums/function-remove-whitespace-string-t14118/)

sid78669 22Sep2008 12:18

Function to remove whitespace from string.
 
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??

oogabooga 22Sep2008 22:00

Re: Function to remove whitespace from string.
 
The first thing we tend to ask is:
What do you have so far?
Where are you stuck?

sid78669 22Sep2008 23:16

Re: Function to remove whitespace from string.
 
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.

oogabooga 23Sep2008 07:20

Re: Function to remove whitespace from string.
 
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[i]) returns true if the character is a space, tab,
vertical tab, form feed, carriage return, or newline. You may find that useful!

sid78669 23Sep2008 07:49

Re: Function to remove whitespace from string.
 
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:

sid78669 23Sep2008 10:37

Re: Function to remove whitespace from string.
 
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);
        }


oogabooga 23Sep2008 19:00

Re: Function to remove whitespace from string.
 
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);
}


sid78669 23Sep2008 23:50

Re: Function to remove whitespace from string.
 
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

oogabooga 24Sep2008 04:29

Re: Function to remove whitespace from string.
 
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;
}



All times are GMT +5.5. The time now is 18:48.