Removing spaces from a "string"

This is a discussion on Removing spaces from a "string" within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm having some serious problems with a peice of work i've been asked to do from a given ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    17

    Removing spaces from a "string"

    Hi all, I'm having some serious problems with a peice of work i've been asked to do from a given design. I have been given the following .h file to use instead of C/C++'s standard header files..

    Code:
    #ifndef MT262ioH
    #define MT262ioH
    #define VCL_IOSTREAM
    #include <vcl.h>
    #include <stdio.h>
    #include <conio.h>
    //---------------------------------------------------------------------------
    //The first pool of functions Write out values of standard types
    //with or without accompanying message (Pr) and with or without
    //following carriage return (Cr). Conventions should be self explanatory.
    
    void WriteStringPrCr(AnsiString Prompt, AnsiString Name);
    void WriteStringPr(AnsiString Prompt, AnsiString Name);
    void WriteStringCr(AnsiString Prompt);
    void WriteString(AnsiString Prompt);
    void WriteIntPrCr(AnsiString Prompt, int N);
    void WriteIntPr(AnsiString Prompt, int N);
    void WriteIntCr(int N);
    void WriteInt(int N);
    void WriteCharPrCr(AnsiString Prompt, char Ch);
    void WriteCharPr(AnsiString Prompt, char Ch);
    void WriteCharCr(char Ch);
    void WriteChar(char Ch);
    void WriteFloatPrCr(AnsiString Prompt, float X);
    void WriteFloatPr(AnsiString Prompt, float X);
    void WriteFloatCr(float X);
    void WriteFloat(float X);
    void WriteDoublePrCr(AnsiString Prompt, double X);
    void WriteDoublePr(AnsiString Prompt, double X);
    void WriteDoubleCr(double X);
    void WriteDouble(double X);
    //------------------------------------------------------------
    //Next pool are corresponding Read functions
    AnsiString ReadStringPr(AnsiString Prompt);
    AnsiString ReadString(void);
    int ReadIntPr(AnsiString Prompt);
    int ReadInt(void);
    char ReadCharPr(AnsiString Prompt);
    char ReadChar(void);
    float ReadFloatPr(AnsiString Prompt);
    float ReadFloat(void);
    double ReadDoublePr(AnsiString Prompt);
    double ReadDouble(void);
    //-----------------------------------------------------
    //One other useful function needed in Block I.
    int Length(AnsiString AString);
    //-------------------------------------------------------
    #endif

    From that I have been asked to write a program that will remove ALL spaces from a program... So far I have this..

    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
    		{
                            //3.4.1     write a single space
    			WriteString(" ");
                            //3.4.2      loop while other spaces found
                            while(Line[Index] == ' ')
    			{
                                    //3.4.3    add to the index thus removing
    				Index = Index + 1;
    			}//3.4.4      loopend
    
    		}//3.5      ifend
    
            }//4       loopend
    
            getchar();
            return 0;
    }
    If you compile and run the above you will notice it cannot remove spaces from the start or the end of the string. Does anyone have any suggestions/areas I could research to find the answer?

    Thanks in advance for any help.

    Regards
    Sy
    Homepage: www.sytaylor.net

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,430
    Unfortunately we cant compile and run the code as we only have the header file, without the implementation of the various functions listed.

    Still, is there a reason for you to write spaces?
    The 3.4.1 comment seems strange, in that respect.

    Also, since you're merely writing the output, wouldnt it be simpler to loop through each character, and ignore spaces?
    Having nested loops seems like overdoing it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Your code is specific to Borland C++ Builder. If you have a look in Borlands Utils unit, you will find an AnsiString string search and replace function. You could call find to find all occurrences of space, and replace them will nothing.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  4. #4
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    I am not sure how your compiler treats Arrays.

    I noticed that you initialize your index value to 1. As far as I know arrays start at element 0. If your compiler treats arrays that way then you may be skipping over the first element.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    You start Index at 1. If the first thing in Line is a space, you don't process it.

    Also, doesn't this line
    Code:
    WriteString(Line[Index]);
    pass a character into the function where a String is required? Not knowing the functions, it looks wrong but might not be.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    Originally posted by laserlight
    Unfortunately we cant compile and run the code as we only have the header file, without the implementation of the various functions listed.

    Still, is there a reason for you to write spaces?
    The 3.4.1 comment seems strange, in that respect.

    Also, since you're merely writing the output, wouldnt it be simpler to loop through each character, and ignore spaces?
    Having nested loops seems like overdoing it.
    Well I'm actually removing "excess" spaces. So for example if someone typed in

    Code:
    "       hello         world      "
    I would in turn output

    Code:
    "hello world"
    Hence some of the complications around the nested loops. As for the header file its all I can find, its a strange course I'm on since i've done some C in the past, at college but this is a part time degree and they insist I do it their way. Thanks for the advice though

    As for setting the index to zero, borland went nuts when i tried to do that and crashed.
    Homepage: www.sytaylor.net

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    Originally posted by WaltP
    Also, doesn't this line
    Code:
    WriteString(Line[Index]);
    pass a character into the function where a String is required? Not knowing the functions, it looks wrong but might not be.
    The function writes a character (in this case) to the screen and the logic dictates this happens when a space is not found. I want it to do that.
    Homepage: www.sytaylor.net

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by sytaylor
    The function writes a character (in this case) to the screen and the logic dictates this happens when a space is not found. I want it to do that.
    You missed my point. Doesn't the definition of WriteString(); require a string as a parameter, not a character? If so, WriteString(Line[Index]); should have an illegal parameter. Or is Line[Index] defined as a AnsiString type?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    Code:
    //Setup Variables
            int Index;
            String Line;
    However, I think "String" may have been defined as an AnsiString in a lib file that they use or something (i'm really no good with header files etc...), since in their header file they're always talking about an AsniString.

    Trust me that line works...

    Its the problem of how to remove leading and following spaces that i struggle with. The program right now works to the point whereby if you had;

    Code:
    "   hello           world    "
    it would output

    Code:
    "   hello world    "
    Homepage: www.sytaylor.net

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    There have been a couple suggestions and we haven't seen any updated code to show any of those changes.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Try a loop, like laserlight said.
    Code:
    char byte;
    char lastChar = '\n';
    for(;; )
    {
        file.read(&byte, 1);
        if(file.eof())
            break;
        
        if(byte == ' ')
        {
            if(lastChar != ' ' && lastChar != '\n')
                outFile.write(&byte, 1);
        }
        else if(byte == '\n')
        {
            if(lastChar != '\n')
                outFile.write(&byte, 1);
        }
    
        lastChar = byte;
    }
    Something along those lines should do it.
    Just Google It. √

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

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    Thanks for the suggestions guys, the reason I'm using a nested loop is that I'm writing from a given design. The best idea i have had to remove preceeding spaces would be to change the bottom loop like this (which works)

    Code:
    else
    		{
                            //3.x if index past first position
                            if (Index > 1)
                            {
            			WriteString(" ");   //3.4.1     write a single space
                            }
                            //3.4.2      loop while other spaces found
                            while(Line[Index] == ' ')
    			{
                                    //3.4.3    add to the index thus removing
    				Index = Index + 1;
    			}//3.4.4      loopend
    
    		}//3.5      ifend
    Homepage: www.sytaylor.net

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    If it works, what's left to do? Unless you're actually reading from a file instead of just one line from the console... In that case, all you have left is to stick the whole thing in a loop anyways...
    Just Google It. √

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

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    17
    I get a compiler error, something like an out or range exception.
    Homepage: www.sytaylor.net

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    while(Line[Index] == ' ')
    {
                 //3.4.3    add to the index thus removing
    	Index = Index + 1;
    }//3.4.4      loopend
    What if the spaces go to the end of the line? Then chances are you'll get array index out of bounds error.

    I'm not sure what would give you a compile-time error though. What line gives you the error?
    Just Google It. √

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

Page 1 of 2 12 LastLast
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, 11:02 AM
  3. Removing spaces
    By chris1985 in forum C Programming
    Replies: 3
    Last Post: 12-22-2004, 08: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, 01:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21