Thread: Removing spaces from a "string"

  1. #16
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    My appologies you are correct its an array out of bounds error. What I don't understand is how its reaching that since the array ubound should be the characters number total including spaces.

    Homepage: www.sytaylor.net

  2. #17
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    Sorry for the shameless bump but i honestly dont understand where im testing/using the array when its out of bounds even when the string has two spaces on the end
    Homepage: www.sytaylor.net

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Sorry for the shameless bump
    Wow, a bump after just an hour and 5 minutes?

    Anyways, what I'm saying is that the loop condition is a bit off. As long as the character you're testing is a space, it will increase index, and check the next charater. If the last character in the string is a space, then it will still increase index, and then check the character one past the end of the string. Actually that shouldn't be a problem if it's a C-style string, since cstrings have a NULL 0 at the end. But if it's an AnsiString, or whatever, it might not work the same way.

    **EDIT**
    Oops yeah, actually 13 hours and 5 minutes. Didn't notice the AM/PM
    Last edited by Hunter2; 03-24-2004 at 10:36 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by Hunter2
    >>Sorry for the shameless bump
    Wow, a bump after just an hour and 5 minutes?

    Anyways, what I'm saying is that the loop condition is a bit off. As long as the character you're testing is a space, it will increase index, and check the next charater. If the last character in the string is a space, then it will still increase index, and then check the character one past the end of the string. Actually that shouldn't be a problem if it's a C-style string, since cstrings have a NULL 0 at the end. But if it's an AnsiString, or whatever, it might not work the same way.
    More like 13 hours and 5 minutes, but still a bump

    You just need to add the logic to that inner while loop to check the length of the string like you did in the outer while loop.

  5. #20
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    Originally posted by jlou
    More like 13 hours and 5 minutes, but still a bump

    You just need to add the logic to that inner while loop to check the length of the string like you did in the outer while loop.
    shameless i know but it worked.. ok i updated it like you said but now im getting compiler errors.

    Code:
    #include <vcl.h>
    //Question Asking Assignment by Simon Taylor 2004
    //TMA01 Part 2, Question 3 A
    
    #include "MT262io.h"
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
    
            //Setup Variables
            int Index;
            String Line;
    
            //1.1    Write Prompt
            WriteString("Enter your text. It must not finish with a space character");
    
            //1.2    Get String with spaces
            Line = ReadStringPr(":");
    
            //1.3    Set Index to 1
            Index = 1;
    
            //2.1
            while(Index <= Length(Line))
            {
                    //3.1   Test position for a space
                    if(Line[Index] != ' ')
                    {
                            //3.2.1     If space not found write the character
                            WriteString(Line[Index]);
                            //3.2.2     and incriment counter
                            Index = Index + 1;
                    }
                    //3.3    if space found
    		else
    		{
    
    
                            if (Index > 1) //Extra Code for Question "B"
                                    {
            	        		WriteString(" ");   //3.4.1     write a single space
                                    }
                                    //3.4.2      loop while other spaces found
    
                                    if(Line[Index] != ' ')  //Extra code for question "C"
                                    {
    
                                            while(Line[Index] == ' ')
    		        	        {
                                                    //3.4.3    add to the index thus removing
            				        Index = Index + 1;
                    			}//3.4.4      loopend
                                               
                                    }//Extra code for question "C"
                    }//else end
    
            }//4       loopend
    
            getchar();
            return 0;
    Edit: Update ok i killed the compiler errors i was having, and now its just not displaying any results at all. crud.. this is the most painful little program i ever wrote.
    Last edited by sytaylor; 03-25-2004 at 04:31 AM.
    Homepage: www.sytaylor.net

  6. #21
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If the following assumptions are true:

    1) String allows use of [] operator to access each char therein
    2) The char array in String is zero based
    3) The char array in String is null terminated
    4) Index represents the index of a given element of the char array

    Then, when Index == Length(Line) we are at the end of the
    char array in Line.

    Also if the += operator is overloaded in String to allow for concatenation of individual char to the end of each String and automatically places the terminal null char at the end of String

    (all of which I believe is true),

    then the following code is an attempt to--

    1) ignore leading spaces
    2) remove more than 1 space between each word in Line
    3) remove any spaces padding the end of line

    Code:
    int Index;
    String Line;
    String newLine;
    
    WriteString("Enter your text.");
     
       
    Index = 0;
    
    bool nonSpaceCharFound = false;
    char ch1;
    char ch2;
    
    //make sure Line isn't empty
    if(Length(Line))
    {
       //eliminate leading spaces
       if(Line[Index] == ' ')
       {
          while(Index < Length(Line) && Line[Index] == ' ')
             Index += 1;
       }
       
       //make sure Line isn't all spaces
       if(Index < Length(Line))
       {   
          //look at all non-null char remaining in Line one at a time
          while(Index < Length(Line))
          {
             if(Line[Index] != ' ')
             {
                //add Line[Index] to end of newLine
                newLine += Line[Index];
                ++Index;
             }
             else
             {
                //find all consecutive spaces
                while(Index < Length(Line) && Line[Index] == ' ')
                    ++Index;
                
                //determine why the loop stopped
                if(Index == Length(Line))
                {   
                    //means these where spaces padding the end of the
                    //Line so ignore them
                }
                else
                {
                    //we're somewhere in the middle of Line
                    //so place one space at the end of newLine to separate
                    // the current word from the next word
                    newLine += ' ';
                    
                    //and add  the current non-space char to the end of Line
                    newLine += Line[Index];
                    
                    //and go to the next char
                    ++Index;
                 }
            }
        }
    }
    Code written during down time at work, compiler not available.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing spaces from a string
    By bradleym83 in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2005, 01:59 PM
  2. Replies: 1
    Last Post: 03-08-2005, 12:02 PM
  3. Removing spaces
    By chris1985 in forum C Programming
    Replies: 3
    Last Post: 12-22-2004, 09:23 AM
  4. k&r ex1-18 removing trailing spaces.
    By xion in forum C Programming
    Replies: 1
    Last Post: 07-14-2003, 02:20 PM
  5. Removing spaces from strings
    By PunkyBunny300 in forum C Programming
    Replies: 6
    Last Post: 02-21-2003, 02:37 PM