Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. removing spaces-newbie

removing spaces-newbie

Scheduled Pinned Locked Moved C / C++ / MFC
question
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    antonaras
    wrote on last edited by
    #1

    Hi guys Lets say i have a file with some text in it that the words are seperated with more than one space in between ex. This___________is_______writen______in____a_file____assume______that__underscores____ are________spaces_____how___can_i___remove____them? i want to store this in a string but in this form: This is writen in a file assume that underscores are spaces how can i remove them? any ideas i tried some things but nothing works thank u in advance

    V 1 Reply Last reply
    0
    • A antonaras

      Hi guys Lets say i have a file with some text in it that the words are seperated with more than one space in between ex. This___________is_______writen______in____a_file____assume______that__underscores____ are________spaces_____how___can_i___remove____them? i want to store this in a string but in this form: This is writen in a file assume that underscores are spaces how can i remove them? any ideas i tried some things but nothing works thank u in advance

      V Offline
      V Offline
      Viorel
      wrote on last edited by
      #2

      In MFC, I think you can read the file in line-by-line manner using CStdioFile class, and then reduce the number of spaces using multiple calls to Replace member of CString class:

      while(s.Replace("__", "_") != 0);
      

      Next, you can write strings to a new file. If you need to store them in the same file, then remove the old file and rename the new one. -- modified at 4:05 Monday 12th June, 2006

      A D 2 Replies Last reply
      0
      • V Viorel

        In MFC, I think you can read the file in line-by-line manner using CStdioFile class, and then reduce the number of spaces using multiple calls to Replace member of CString class:

        while(s.Replace("__", "_") != 0);
        

        Next, you can write strings to a new file. If you need to store them in the same file, then remove the old file and rename the new one. -- modified at 4:05 Monday 12th June, 2006

        A Offline
        A Offline
        antonaras
        wrote on last edited by
        #3

        Thanks for the reply Viorel I'm not using MFC so is it possible to use the functions that u mention. also using while(s.Replace(" ", " ") != 0);//i'm going to loose all spaces i need to leave only one. thanks again Viorel

        S T V 3 Replies Last reply
        0
        • A antonaras

          Thanks for the reply Viorel I'm not using MFC so is it possible to use the functions that u mention. also using while(s.Replace(" ", " ") != 0);//i'm going to loose all spaces i need to leave only one. thanks again Viorel

          S Offline
          S Offline
          Sarath C
          wrote on last edited by
          #4

          u can use strtok function to remove or find special character in a given string. using simlple logic, u can make it suit for ur purpose. strtok, wcstok, _mbstok[^] SaRath.
          "Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++

          A 1 Reply Last reply
          0
          • A antonaras

            Thanks for the reply Viorel I'm not using MFC so is it possible to use the functions that u mention. also using while(s.Replace(" ", " ") != 0);//i'm going to loose all spaces i need to leave only one. thanks again Viorel

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            use [std::string::replace()](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_string_basicstringreplace.asp)[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_string_basicstringreplace.asp "New Window")] instead...


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            S 1 Reply Last reply
            0
            • T toxcct

              use [std::string::replace()](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_string_basicstringreplace.asp)[[^](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vclrf_string_basicstringreplace.asp "New Window")] instead...


              TOXCCT >>> GEII power

              [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

              S Offline
              S Offline
              Sarath C
              wrote on last edited by
              #6

              good solution. thanks for the info :) SaRath.
              "Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++

              1 Reply Last reply
              0
              • A antonaras

                Thanks for the reply Viorel I'm not using MFC so is it possible to use the functions that u mention. also using while(s.Replace(" ", " ") != 0);//i'm going to loose all spaces i need to leave only one. thanks again Viorel

                V Offline
                V Offline
                Viorel
                wrote on last edited by
                #7

                If you do not use MFC, you can use STL. You can read the file using a stream object like ifstream, store strings in string object, and use find function to find a sequence consisting of two spaces. Then use find_first_not_of to find the end of sequence, and call erase to remove unneeded sequences. If you do not use STL, you can use library functions: fgets to get strings, strstr or strchr to find spaces, and so on. If you need to do this manually, you can open the file in Notepad and use Replace command

                antonaras wrote:

                i'm going to loose all spaces i need to leave only one.

                The first parameter of Replace contains two spaces, and the second one -- a single space, so any sequence of more then one spaces will be replaced with only one (maybe not so fast as possible). For convenience, I modified the sample using underscores.

                A 1 Reply Last reply
                0
                • S Sarath C

                  u can use strtok function to remove or find special character in a given string. using simlple logic, u can make it suit for ur purpose. strtok, wcstok, _mbstok[^] SaRath.
                  "Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++

                  A Offline
                  A Offline
                  antonaras
                  wrote on last edited by
                  #8

                  Hey Sarath thanks for the reply can i ask u a couple of questions on strtok? the link u gave me has an example of using strtok char string[] = "A string\tof ,,tokens\nand some more tokens"; char seps[] = " ,\t\n"; char *token; can i use a string instead of char string[]? also can i put any number of deliminars in seps? thanks a lot for the help

                  T 1 Reply Last reply
                  0
                  • V Viorel

                    If you do not use MFC, you can use STL. You can read the file using a stream object like ifstream, store strings in string object, and use find function to find a sequence consisting of two spaces. Then use find_first_not_of to find the end of sequence, and call erase to remove unneeded sequences. If you do not use STL, you can use library functions: fgets to get strings, strstr or strchr to find spaces, and so on. If you need to do this manually, you can open the file in Notepad and use Replace command

                    antonaras wrote:

                    i'm going to loose all spaces i need to leave only one.

                    The first parameter of Replace contains two spaces, and the second one -- a single space, so any sequence of more then one spaces will be replaced with only one (maybe not so fast as possible). For convenience, I modified the sample using underscores.

                    A Offline
                    A Offline
                    antonaras
                    wrote on last edited by
                    #9

                    Thanks Viorel your answer is very clear and helpful appriciate all the help

                    1 Reply Last reply
                    0
                    • A antonaras

                      Hey Sarath thanks for the reply can i ask u a couple of questions on strtok? the link u gave me has an example of using strtok char string[] = "A string\tof ,,tokens\nand some more tokens"; char seps[] = " ,\t\n"; char *token; can i use a string instead of char string[]? also can i put any number of deliminars in seps? thanks a lot for the help

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #10

                      did you read my replies ? :~


                      TOXCCT >>> GEII power

                      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                      1 Reply Last reply
                      0
                      • V Viorel

                        In MFC, I think you can read the file in line-by-line manner using CStdioFile class, and then reduce the number of spaces using multiple calls to Replace member of CString class:

                        while(s.Replace("__", "_") != 0);
                        

                        Next, you can write strings to a new file. If you need to store them in the same file, then remove the old file and rename the new one. -- modified at 4:05 Monday 12th June, 2006

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        Viorel. wrote:

                        while(s.Replace("__", "_") != 0);

                        The loop is not required.


                        "The largest fire starts but with the smallest spark." - David Crow

                        "Judge not by the eye but by the heart." - Native American Proverb

                        V 1 Reply Last reply
                        0
                        • D David Crow

                          Viorel. wrote:

                          while(s.Replace("__", "_") != 0);

                          The loop is not required.


                          "The largest fire starts but with the smallest spark." - David Crow

                          "Judge not by the eye but by the heart." - Native American Proverb

                          V Offline
                          V Offline
                          Viorel
                          wrote on last edited by
                          #12

                          I think the loop is required. Otherwise, in case of three consecutive spaces, only first two ones will be replaced with one space, and we will obtain two spaces in the result. Therefore we have to repeat the replace operation. It is possible to avoid loop and improve performance using other approaches.

                          D 1 Reply Last reply
                          0
                          • V Viorel

                            I think the loop is required. Otherwise, in case of three consecutive spaces, only first two ones will be replaced with one space, and we will obtain two spaces in the result. Therefore we have to repeat the replace operation. It is possible to avoid loop and improve performance using other approaches.

                            D Offline
                            D Offline
                            David Crow
                            wrote on last edited by
                            #13

                            Viorel. wrote:

                            I think the loop is required.

                            You're right. I spoke prematurely. :-O Sorry about that.


                            "The largest fire starts but with the smallest spark." - David Crow

                            "Judge not by the eye but by the heart." - Native American Proverb

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups