Thread: Check for blank spaces in a string

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    47

    Check for blank spaces in a string

    In c++ I know you could check for black space by

    Code:
    if(string[i]==' ')
    In c this is not working for me. Does c represent a blank space differently?

    My code looks like this but the blank spaces are not skipped.

    Code:
     while (string[i] == ' '){ i++;}

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I think you are withholding the context in which your bug lies.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    If you're trying to 'skip' blank spaces, tell me, what do you think will happen if you have this string below, and only use that while loop once?

    "This is a string"

    Nothing. It checks the condition - it won't match if i is zero at the beginning, and as so, it will not increment i and then it will execute whatever comes next in your code.

    Please include more code.

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    NVM
    edit

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    I guess I should re-explain. I am trying to parse out a simple math expressions such as 3+5. So I have 3 while loops skipping out blank spaces. So I have a while to skip blank in the case of " 3+5" and then I have another while loop after the first integer is finished in the case of "3 +5" and then I have a while loop after the sign to skip for spaces in the case of "3+ 5". Again for some reason the while loop NEVER runs. Why does string[i] == ' ' not work?

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I would suggest you reconsider your approach.

    How about one subroutine that strips out ALL blanks and puts the blank-less expression is another buffer?

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Quote Originally Posted by Todd Burch View Post
    I would suggest you reconsider your approach.

    How about one subroutine that strips out ALL blanks and puts the blank-less expression is another buffer?

    Todd
    I have my reasons that it needs to be done this way. Can we get back to the orignial problem of why ' ' will not pick up as a blank in a while loop? Any way that I try to code this I will have to check for ' '.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The code as you have posted it works (assuming that you only want to skip blanks -- not strip them out -- at the point i and not elsewhere, as mentioned above). Consequently, if you have an error it is elsewhere.

  9. #9
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by BENCHMARKMAN View Post
    I have my reasons that it needs to be done this way. Can we get back to the orignial problem of why ' ' will not pick up as a blank in a while loop? Any way that I try to code this I will have to check for ' '.
    What reasons could you possibly have for doing it the 'wrong way' ?

    Also, how can you be so arrogant when we try to help you when you haven't even bothered to post the code?

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Quote Originally Posted by IceDane View Post
    What reasons could you possibly have for doing it the 'wrong way' ?

    Also, how can you be so arrogant when we try to help you when you haven't even bothered to post the code?
    I'm not being arrogant, its a requirment and I fully understand the reasons. If you don't want to help me out just because I can't and won't code it your way then fine. Again no matter how I write the code it I will have to look for a blank space. So again I ask how to you see if a character is equal to a blank space in c if string[i] == ' ' does not work?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BENCHMARKMAN View Post
    I'm not being arrogant, its a requirment and I fully understand the reasons. If you don't want to help me out just because I can't and won't code it your way then fine. Again no matter how I write the code it I will have to look for a blank space. So again I ask how to you see if a character is equal to a blank space in c if string[i] == ' ' does not work?
    And what does this code do for you?
    Code:
    #include <stdio.h>
    int main()
    {
      const char *string = " A string that starts with a space.";
      int i=0;
    
      while (string[i] == ' ') {printf("Found it!\n"); i++;}
      return 0;
    }
    If it doesn't print "Found it!", then I think you can legitimately say you have found a bug in your compiler.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by BENCHMARKMAN View Post
    I'm not being arrogant, its a requirment and I fully understand the reasons. If you don't want to help me out just because I can't and won't code it your way then fine. Again no matter how I write the code it I will have to look for a blank space. So again I ask how to you see if a character is equal to a blank space in c if string[i] == ' ' does not work?
    That's what your missing! That works fine in C. It won't do what you need, however. As mentioned above, the VERY FIRST time your string[i] is not a blank space, your while loop will end. So "This should work", with the i == 0, will find the 'T', and kick out of the while loop. No spaces will be found in the string.

    Your while loop WILL trim off leading spaces before the first char, if you add some code to do that, but that's about all it can do.

    I suggest something more like:

    Code:
    s_length = strlen(string);
    while( i < s_length)  {
       if(string[i++] == ' ')
          blank_num++;  //whatever 
    }
    But the answer to your question is yes, if(string[i] == ' ') works in C, just fine.
    Last edited by Adak; 03-11-2008 at 08:14 PM.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    My Hat is feeling bored, so I will pass along some suggestions:
    (1) There is a function isspace, which will also find things like tab characters and carriage returns, if you're reading from a file or you have the kind of user who will hit tab while they're typing.
    (2) scanf will stop reading once you get to a space, so hopefully you're using fgets.
    (3) Most likely, i is not the number you think it is. Do you have i re-defined in a for loop (or local to a function) so that it gets reset when the for-loop (or function) ends? Is your number-getting routine too greedy, so that it consumes one extra character more than it should?

    That's the best we could come up with, given the context (specifically: none). If you want better hints, you'll have to provide better context.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Quote Originally Posted by tabstop View Post
    My Hat is feeling bored, so I will pass along some suggestions:
    (1) There is a function isspace, which will also find things like tab characters and carriage returns, if you're reading from a file or you have the kind of user who will hit tab while they're typing.
    (2) scanf will stop reading once you get to a space, so hopefully you're using fgets.
    (3) Most likely, i is not the number you think it is. Do you have i re-defined in a for loop (or local to a function) so that it gets reset when the for-loop (or function) ends? Is your number-getting routine too greedy, so that it consumes one extra character more than it should?

    That's the best we could come up with, given the context (specifically: none). If you want better hints, you'll have to provide better context.
    I think you found my problem, I was using scanf. Thanks you are the man! Thanks for the isspace function suggestion too.

    For the rest of you again this would have worked PERFECTLY had I used fgets. So please next time don't tell me something is wrong when its not.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BENCHMARKMAN View Post
    In c++ I know you could check for black space by

    Code:
    if(string[i]==' ')
    In c this is not working for me. Does c represent a blank space differently?
    [/CODE]
    Quote Originally Posted by BENCHMARKMAN View Post
    So please next time don't tell me something is wrong when its not.
    Wait -- you told us it wasn't working, we told you it was; why complain about that when we turn out to be right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM