Closed Thread
Results 1 to 8 of 8

Thread: removing white space, the project has defeated me

  1. #1
    Hawk1 is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    70
    Rep Power
    0

    removing white space, the project has defeated me

    So i have this assignment to delete white space from a sentence using a pointers, well i was excited thinking the program was going to work first try, becuase i had no errors. WELL i was wrong........


    heres the assignment:

    Using pointers write a function called stripWhite that will strip all of the white spaces from an array of characters. Your function should return the number of white spaces that you deleted.

    White spaces can be a number of characters, but in our case we are only concerned with the blank space character (space bar) which is the character Decimal 32 or Hex 20.

    Your prototype should be something like:

    int stripWhite(char *str);

    Your output should look something like the following:

    Enter a sentence and I will strip out all spaces.
    The quick brown fox jumped over the lazy old dog
    Your sentence without spaces is:
    Thequickbrownfoxjumpedoverthelazyolddog.


    Note: Passing an array does not count as using pointers. So, if your only use of pointers is this:



    int stripWhite(char *str)
    {

    }



    Think again!
    heres what i had for code
    Code:
    #include <iostream> 
    #include <iomanip> 
    #include <cstdlib> 
    
    
    
    using namespace std; 
    int stripWhite(char *str);
    
    
    
    int main()
    {
        char s;
        char *str = &s;
        char wo;
        
            cout << "Enter a sentence: ";
            cin >> *str;
    
            wo = stripWhite(str);
    
            cout << "Without Spaces:" << wo << endl;
            
    
       return 0;
    }
    
    
    
    int stripWhite(char *str)
    {
        char *p;
    
        p = str;
        do 
        if (!isspace(*p=*str))p++; 
        while (*str++);    
    
        return *str;
        
    }
    can anyone help me with what i have done wrong? i get this message (refer to pic below)
    thank you everyone
    rep for help!
    Attached Thumbnails Attached Thumbnails removing white space, the project has defeated me-picta.jpg  

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    atomicbaum is offline Newbie
    Join Date
    Oct 2008
    Location
    Spokane WA
    Posts
    5
    Rep Power
    0

    Re: removing white space, the project has defeated me

    The first problem you have in your program is using std::cin which only inputs a word not a sentence. So if you input "Hello my name is bob\0", you would only get "Hello\0". Try using std::cin.getline( str, 1024 ). The next problem is you are assigning the pointer str to point to s. This gives you one character of memory. If you enter anything even one letter, you will overflow the allocated memory because cin tacks on a "\0" (null character) at the end. Allocate an array like str[ 1024 ] and use it just like a pointer because arrays are secretly pointers. Another thing that would mess you up is the character wo. your function returns the number of characters deleted, so wo needs to be an int not a character or string. Remember to count the whitespace. I also left comments in some code i changed. I don't want to do your whole assignment for you, but you need to rethink the way your whitespace removing works. Remember str is a pointer to an array of characters so you can use pointer arithmetic to move through the array. once you've found the whitespace, try moving everything in the array back one.
    Hope this helps a little. If you get stuck again, post it and I can get back to you. Its late and I'm going to sleep.

    Code:
    #include <iostream> 
    #include <iomanip> 
    #include <cstdlib> 
    
    using namespace std; 
    int stripWhite(char *str);
    
    
    
    int main()
    {
      char str[ 1024 ]; // changed to an array
      int whitespace; // replaced wo with whitespace
        
            cout << "Enter a sentence: ";
            cin.getline( str, 1024 ); // cin.getline to get the whole line
    
            whitespace = stripWhite(str); // returns # of whitespaces
    
    	// would print out # of whitespaces, not the 
    	// string itself, which is what you would want
            //cout << "Without Spaces:" << wo << endl;
            
    
       return 0;
    }
    
    
    
    int stripWhite(char *str)
    {
        char *p;
        p = str;
        int whitespace = 0; // whitespace counter, dont forget
    
        // try to rethink this area between here
        //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV 
        do 
          {
    	// assigns the value of str to the value of p
    	// doesn't do what you think here
    	if (!isspace(*p=*str))  // remember to increase whitespace counter
    	  p++; // since p is a pointer, moves up one (one from start
    	       // of str)
          }while ( *str++ ); // very good use of pointer arithmetic!, but
                             // you lose your spot   
        //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        // and here
        return whitespace; // return the number of whitespaces
        
    }

  4. #3
    Hawk1 is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    70
    Rep Power
    0

    Re: removing white space, the project has defeated me

    dang i wasnt even thinking about the cin part! that makes sense now.

    ill tackle this later todya, its 5am and i have to go to work. UGH

  5. #4
    Hawk1 is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    70
    Rep Power
    0

    Re: removing white space, the project has defeated me

    well now its just a matter of getting the code to run according to the assignment. thats the hard part.

    i feel lost on this project for some reason. lol

  6. #5
    Feral is offline Programmer
    Join Date
    Jul 2008
    Posts
    163
    Rep Power
    15

    Re: removing white space, the project has defeated me

    Well as atomicbaum said, your original code will cause a buffer overflow problem with entering even one character. So using getline with a array of characters would be your best bet. I don't see why you are using the do while loop when i basic for loop would be more relevant (at least to me).

    Also in the instructions it says to print out the string with no spaces and the number of spaces found. In your code you could only do one of these.

    You could do something like this that would find any space character in the string and get rid of it while counting the number of times it came across a space.
    Code:
    #include <iostream> 
    #include <iomanip> 
    #include <cstdlib> 
    
    
    
    using namespace std; 
    void stripWhite(char *str);
    
    
    
    int main()
    {
    	char str[256];
        
            cout << "Enter a sentence: ";
            cin.getline(str,256);
    
           stripWhite(str);       
    
       return 0;
    }
    
    
    
    void stripWhite(char *str)
    {
    	char *p;
    	p = str;
    	int spaces = 0;
    	int size = strlen(p);
    
    	cout<<"your string without spaces is ";
    
        for(int i = 0; i < size; i++)
    	{
    		if(!isspace(p[i]))
    		{
    			cout<< p[i] ;
    			//spaces++;
    		}
    		else
    		{
    			spaces++;
    		}
    	}
    	
    	cout<< endl <<"there where "<< spaces <<" spaces" <<endl;
        
    }
    Or since the instructions said that you where only interested in the space bar character you could just do
    Code:
    #include <iostream> 
    #include <iomanip> 
    #include <cstdlib> 
    
    
    
    using namespace std; 
    void stripWhite(char *str);
    
    
    
    int main()
    {
    	char str[256];
        //char *str = &s;
        
            cout << "Enter a sentence: ";
            cin.getline(str,256);
    
           stripWhite(str);       
    
       return 0;
    }
    
    
    
    void stripWhite(char *str)
    {
    	char *p;
    	p = str;
    	int spaces = 0;
    	int size = strlen(p);
    
    	cout<<"your string without spaces is ";
    
        for(int i = 0; i < size; i++)
    	{
    		//0x20 is the ascii code for the space bar
    		if(p[i] != 0x20)
    		{
    			cout<< p[i] ;
    			//spaces++;
    		}
    		else
    		{
    			spaces++;
    		}
    	}
    	
    	cout<< endl <<"there where "<< spaces <<" spaces" <<endl;
        
    }
    Which would only look for the space bar (0x20)

  7. #6
    Hawk1 is offline Learning Programmer
    Join Date
    Sep 2008
    Posts
    70
    Rep Power
    0

    Re: removing white space, the project has defeated me

    i think my best route is to look for any spaces in the sentences only, after reading all your code, this project really makes sense now. its always easy to get it right after someone helps you though huh? haha well i appreciate all your help

  8. #7
    Feral is offline Programmer
    Join Date
    Jul 2008
    Posts
    163
    Rep Power
    15

    Re: removing white space, the project has defeated me

    Glad I could be of assistance, tho normally I would just point in the right direction instead of posting complete code but hey I was board lol.

  9. #8
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: removing white space, the project has defeated me

    Since others have already posted code to look at, here is more to compare/contrast with.
    Code:
    #include <iostream> 
    #include <cctype> 
    using namespace std; 
    
    int stripWhite(char *str);
    
    /**
     * This skips user input and simply provides a canned test driver.
     */
    int main()
    {
       char text[] = "The quick brown fox jumps over the lazy dog";
       cout << text << '\n';
       stripWhite(text);
       cout << text << '\n';
       return 0;
    }
    
    /**
     * Strip whitespace from a string. Note that the source and destination overlap.
     */
    int stripWhite(char *src)
    {
       char *dst;           // Use this pointer to write non-whitespace.
       for ( dst = src;     // Point to the start of the source string.
             *src != '\0';  // Continue loop for all text in source string.
             ++src )        // Always increment source pointer.
       {
          if ( !isspace(*src) )
          {
             *dst++ = *src; // Write and increment only if not whitespace.
          }
       }
       *dst = '\0';         // Ensure the modified string is null terminated.
    }
    
    /* my output
    The quick brown fox jumps over the lazy dog
    Thequickbrownfoxjumpsoverthelazydog
    */
    The key here is that writing to the destination is selective -- only if it is not whitespace does it copy the character pointed to by src into the location specified by dst, and the dst pointer is only incremented after it has done so.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Pokemon Black and White
    By stevie754 in forum Video Game Talk
    Replies: 0
    Last Post: 03-18-2011, 02:22 AM
  2. strip white space
    By zeroradius in forum PHP Development
    Replies: 6
    Last Post: 07-01-2009, 08:10 AM
  3. White screen after logged in
    By gamiR in forum Linux Applications
    Replies: 2
    Last Post: 12-08-2008, 06:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts