Thread: C: removing extra spaces in line

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    87

    C: removing extra spaces in line

    Hi,
    My goal is to remove extra spaces from line.

    I've tried multiple ways, many variables. But I get error for seg fault.

    My code:

    Code:
      unsigned int j=0;                                                                                                                                                                            
      char *line="a    good";                                                                                                                                                                      
      char *temp;                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                      
      while(*(line+j)){                                                                                                                                                                            
                                                                                                                                                                                                   
        if(*(line+j)==' ' && *(line+j+1) ==' ' ){                                                                                                                                                  
          temp=line+j+1;                                                                                                                                                                           
                                                                                                                                                                                                   
          while(*(temp)!='\0'){                                                                                                                                                                    
            *(temp-1)=*(temp);                                                                                                                                                                     
            temp++;                                                                                                                                                                                
                                                                                                                                                                                                   
          }                                                                                                                                                                                        
        }                                                                                                                                                                                          
                                                                                                                                                                                                   
        j++;                                                                                                                                                                                       
      }
    Cannot understand my bug.
    Sorry for naive problem.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    You're attempting to modify a string literal, which is a no-no. Using "temp" as an intermediary pointer just hides this fact.

    Try changing "line" to a suitably sized array and see what happens.

    I suspect you did not come up with an algorithm before writing this code, since the results are incorrect. I suggest planning this out on paper before trying to implement it in code.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    87
    Quote Originally Posted by Matticus View Post
    You're attempting to modify a string literal, which is a no-no. Using "temp" as an intermediary pointer just hides this fact.

    Try changing "line" to a suitably sized array and see what happens.

    I suspect you did not come up with an algorithm before writing this code, since the results are incorrect. I suggest planning this out on paper before trying to implement it in code.
    Hi Matt,
    Thanks for your reply.
    Code:
      unsigned int j=0;                                                                                                                                                                            
      char line[]="a    good";                                                                                                                                                                     
      char *temp;                                                                                                                                                                                  
                                                                                                                                                                                                   
      while(*(line+j)){                                                                                                                                                                            
                                                                                                                                                                                                   
        if(*(line+j)==' ' && *(line+j+1) ==' ' ){                                                                                                                                                  
                                                                                                                                                                                                   
          temp=line+j+1;                                                                                                                                                                           
          while((*temp++)!='\0'){                                                                                                                                                                  
            *(temp-1)=*(temp);                                                                                                                                                                     
                                                                                                                                                                                                   
          }                                                                                                                                                                                        
        }                                                                                                                                                                                          
        else{                                                                                                                                                                                      
          j++;                                                                                                                                                                                     
                                                                                                                                                                                                   
        }                                                                                                                                                                                          
                                                                                                                                                                                                   
      }                                                                                                                                                                                            
                                                                                                                                                                                                   
      line[j]='\0';
    If I create a function, and pass a string literal then it would fail too, I believe. Because that is my next step.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Quote Originally Posted by deathmetal View Post
    If I create a function, and pass a string literal then it would fail too, I believe. Because that is my next step.
    Yes. As I said, you cannot modify a string literal. You would need to pass a modifiable value to this function, such an an array or a pointer to allocated memory.
    Last edited by Matticus; 03-22-2016 at 08:19 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    A style recommendation: use line[j] instead of *(line+j). That said, I would have two pointers: one points to the current position to write, the other points to the current position to read (as in examine the value of the character to see if it is a space or not). As reading progresses, I increment the read pointer; as writing progresses, I increment the write pointer. I would also have a flag that is set whenever a space is encountered, and unset whenever a non-space is encountered, and based on this flag I can determine whether to skip a space that was just read, or to write it. With this approach, I would not need nested loops: the loop "reads" the input string character by character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-29-2011, 07:02 AM
  2. _wfopen and extra spaces
    By Ducky in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2011, 09:45 PM
  3. Eliminating extra spaces between words
    By learninC in forum C Programming
    Replies: 8
    Last Post: 03-15-2005, 02:20 PM
  4. Removing spaces
    By chris1985 in forum C Programming
    Replies: 3
    Last Post: 12-22-2004, 09:23 AM
  5. deleting extra spaces
    By girlzone in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2003, 02:06 PM

Tags for this Thread