Getting rid of spaces in a string

This is a discussion on Getting rid of spaces in a string within the C Programming forums, part of the General Programming Boards category; I have to read a string and get rid of spaces before the first letter of the string, and multiple ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    19

    Getting rid of spaces in a string

    I have to read a string and get rid of spaces before the first letter of the string, and multiple spaces. I want to do it with a for loop.


    So I have change this(for example):


    ***this***** is ******..........*******test******\0

    (* = space)

    To this:


    this is a test \0



    I'm also not too familiar with the isspace function, so I don't want to use that.


    Yes, it's homework. But it is just a small part of the program I'm having trouble with.


    Thanks.


    Edit: Won't allow whitespace. I used asterisks to show whitespace.
    Last edited by David101; 09-23-2004 at 02:16 PM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Since its homework I'll just tell you how I normally do it. There are two ways I prefer:

    1. If you know its a small string, have a temporary buffer where text will be read and only regular characters and one space is copied into the temp buffer. The temp buffer can then overwrite the original text.

    2. Check to for spaces, if more than one space is found move the entire string up to where the first space was. i.e.

    this******is

    ptr1 is at the first '*'
    ptr2 found 'i'
    now memmove() the string from ptr2 down to ptr1+1

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    19
    How do you suggest I get rid of the space before the first letter?

    If I copy a part of the string, won't the next time I copy something onto the buffer it will delete whatever I previously copied.

    It's not like I can have an infinte number of buffers. Plus the string size is unknown.


    I don't understand why this is giving me such a hard time.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Having 2 buffers would probably be the quickest method. You need 2 buffers and 2 counters.

    Code:
    for(counter1 = 0;buffer1[counter1];++counter1)
    {
      if(isspace(buffer[counter1]) && (isspace(buffer[counter1 + 1]) || counter2 == 0))
      {
        continue;
      }
      buffer2[counter2++] = buffer1[counter1];
    }
    Something along those lines maybe.
    Last edited by itsme86; 09-23-2004 at 04:07 PM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    19
    Quote Originally Posted by itsme86
    Having 2 buffers would probably be the quickest method. You need 2 buffers and 2 counters.

    Code:
    for(counter1 = 0;buffer1[counter1];++counter1)
    {
      if(isspace(buffer[counter1]) && (isspace(buffer[counter1 + 1]) || counter2 == 0))
      {
        continue;
      }
      buffer2[counter2++] = buffer1[counter1];
    }
    Something along those lines maybe.

    The problem is I can't use isspace because I'm not familiar with it.

    Heres a link to my assignment if anyone cares:


    http://cs.senecac.on.ca/~oop244/assi...-v1-20043.html

    Scroll down to the int oneSpace(char s[])

    Yes, the first assignment is in C, future assignments are in C++. Stupid they put C++, but whatever.


    I'm tearing my hair out over this.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Just replace isspace(x) with x == ' ' <--- There's a space between the quotes here.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I have to read a string and get rid of spaces before the first letter of the string, and multiple spaces.

    for the second part you might try something like this:

    create a counter and set it to zero. on each iteration of the loop, if the character is a space increment the counter, otherwise set it to zero. as long as the counter is less than or equal to one, copy the character.

    >> Having 2 buffers would probably be the quickest method

    you really don't need two buffers for this sort of operation. consider a function that removes newlines from a string:

    Code:
    char * remove_newlines(char * str) {
     char * s, * d;
     for(s = d = str; *s; ++s) {
      if(*s != ‘\n’) {
       *d++ = *s;
       } 
      }
     *d = 0;
     return str;
     }
    the very nature of the operation makes it much easier to work with the same string for both reading and writing.

    >> I don't understand why this is giving me such a hard time.

    post what you have so far.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    19
    Quote Originally Posted by Sebastiani
    post what you have so far.
    Nothing so far, cause I didn't know where to even originally start.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Well, I'd start with a function declaration called oneSpace() that conforms to the specifications outlined in your assignment.
    If you understand what you're doing, you're not learning anything.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well I guess you'd better get busy then. I'd suggest writing out on paper what you need to do. Break it down into big steps, then once you have that done, break each step down into smaller steps until you know exactly what you need to do.

    I'm beginning to sound like a broken record.

    Quzh.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Instead of isspace() you can always check to see if the value is less than or equal to space (since all white space has an ascii value lesser than 32). Also, the double buffer thing is best in my opinion too. But it becomes complicated when double buffering a large amount of text (hint: process the large body of text in segments).

    Additionally, you commented on the space before the text as well. Removing white space around a string of text is easier than compacting whitespace. Come on man, I think you can handle that one yourself.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    19
    Quote Originally Posted by master5001
    Additionally, you commented on the space before the text as well. Removing white space around a string of text is easier than compacting whitespace. Come on man, I think you can handle that one yourself.
    Actually I have never been taught how to remove unwanted characters like multiple spaces out of a string. It is difficult for me.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Try not to think of it as removing spaces from the string as much as running the string through a filter that catches all the whitespace.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    19
    Yes, but what do with the whitespace. I don't know how to get completely rid of it from the string.

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Yes, but what do with the whitespace

    look at the code for the 'remove_newlines' function I posted - can you see how the function works? here's another version using two buffers and no pointers:

    Code:
    char * remove_newlines(char * str) {
     int s, t;
     char * temp = malloc(strlen(str)+1);
     if(temp != NULL) {  
      for(s = t = 0; str[s] != 0; ++s) {
       if(str[s] != ‘\n’) {
        temp[t] = str[s];
        t++; 
        } 
       }
      temp[t] = 0;
      strcpy(str, temp);
      free(temp); 
      }
     return str;
     }
    does that make sense?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. removing spaces from a string
    By bradleym83 in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2005, 02:59 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

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