Thread: Remove white space problem

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    Remove white space problem

    Hi, I am new to programming and I am trying to figure out what I am doing wrong with my code. I want to be able to remove spaces from a string. Using my code I get ! in places where I have white space instead of moving the next character to the position of the white space. Can anyone tell me what I am missing?


    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    #include <math.h>
    #include <string.h>
    
    int main()
    {
     const int MAXCHARS = 71;
     char temp[MAXCHARS];
     char words[MAXCHARS] = "M y na me is J o hn ";
     int i;
     
     for(i=0; i < MAXCHARS; i++)
     {	 
     	 for(i=0; i < MAXCHARS; i++)
     	 {
    
      		  if (words[i] == 32)  
    		  {
    		  temp[i] = words[i] + 1 ;
     		  }
     		  else
    		   temp[i] = words[i]; 
    
         }
     cout << temp << endl;
    
     getch();
      
    }
     return 0;
     
    }

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    i am not quite sure about this but you can try
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    #include <math.h>
    #include <string.h>
    
    int main()
    {
     const int MAXCHARS = 71;
     char temp[MAXCHARS];
     char words[MAXCHARS] = "M y na me is J o hn ";
     int i;
     
     for(i=0; i < MAXCHARS; i++)
     {	 
     	 for(i=0; i < MAXCHARS; i++)
     	 {
    
      		  if (words[i] == char(32))  
    		  {
    		  temp[i] = words[i] +1  ;
     		  }
     		  else
    		   temp[i] = words[i]; 
    
         }
     cout << temp << endl;
    
     getch();
      
    }
     return 0;
     
    }

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    That gives me the same result. I tried that before. I think it has something more to do with the
    Code:
     temp[i] = words[i] +1  ;
    portion.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Why do you have two loops that use the same variable i?

    You are getting !'s because you are adding 1 to the value of words[i], and 33 is the value of an exlamation point. What you want is temp[i] = words[i+1].

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    alrighty, I changed it but now I get the following display MyynnammeiisJJoohhn


    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    #include <math.h>
    #include <string.h>
    
    int main()
    {
     const int MAXCHARS = 71;
     char temp[MAXCHARS];
     char words[MAXCHARS] = "M y na me is J o hn ";
     int i, j;
     
     for(j=0; j < MAXCHARS; j++)
     {	 
     	 for(i=0; i < MAXCHARS; i++)
     	 {
    
      		  if (words[i] == 32)  
    		  {
    		  temp[i] = words[i+1]  ;
     		  }
     		  else
    		   temp[i] = words[i]; 
    
         }
     cout << temp << endl;
    
     getch();
      
    }
     return 0;
     
    }

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Its because when you hit a space, you store the next letter, move up one, then store the letter again. Instead of storing the next letter, you are better just not storing anything when you hit a space.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    20
    You'[ve still got a loop that you aren't using (the for(j...) one).

    The other problem is that you're not actually shifting the data you're only copy the next byte in when you have a space. This is how I would do it:

    Code:
    for(i=0, j=0; i<MAX_CHARS; i++)
       if(words[i]!=' ')
          words[j++]=words[i];

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    Thanks that did it!

  9. #9
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    There's a reason for the STL functios
    Code:
    #include <iostream>
    #include <string>
    
    #define Trim(pointer1,pointer2) tTrim<string>(pointer1, pointer2)
    
    template <typename T, typename I>
    T tTrim(I i, I f)
    {
         T buff;
         std::remove_copy_if(i, f, std::back_insert_iterator<T>(buff), std::bind2nd(std::equal_to<char>(),' '));
         return buff;    
    }
    
    int main()
    {
        using namespace std;
        string str, buff;
        cout << "Input a string with spaces: "; getline(cin, str);
    	buff = Trim(str.begin(), str.end());
        cout << "The same string w/o spaces: " << buff << endl;
        system("pause");
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading string with white space in C
    By gep in forum C Programming
    Replies: 2
    Last Post: 05-30-2009, 05:18 PM
  2. Cin newline space problem... or something
    By Baqualish in forum C++ Programming
    Replies: 10
    Last Post: 10-17-2007, 03:49 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Firing problem in space sim
    By VirtualAce in forum Game Programming
    Replies: 7
    Last Post: 03-29-2005, 12:14 PM
  5. Remove space in a string
    By alice in forum C Programming
    Replies: 37
    Last Post: 05-18-2004, 12:27 AM