Thread: Check char for a space or for end of array?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    16

    Check char for a space or for end of array?

    Hey,

    In my program to turn an infix statement into a postfix or prefix statement, I want to check if a character from my input is equal to a space OR if it is equal to the end of the array.

    So basically, I have a char array and Im picking off one char at a time, then comparing it.

    So, how do I say:

    if(array[i] == ' ') ?

    and

    if(array[i] == '/0') ?

    I dont think those are correct, but I think thats why my program is messing up.

    Thanks,
    BC

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Those check for a space and the null-terminating character of a string. Do you know that there is a '\0' at the end of your input? Also, what if there's a weird character like '\n', is that handled?

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    if(array[i] == ' ' || array[i] == '\0')
    {
       /* space or end of array */
    }
    It really depends on your loop though,
    Code:
    size_t len = strlen(array);
    for(i = 0; i < len; i++)
    {
       /* array[i] will never be '\0' */
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Post your code.
    Last edited by Sebastiani; 11-15-2008 at 05:42 PM. Reason: speling
    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;
    }

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    Awesome...I dont really have it inside a loop. Im calling a getChar function inside a while loop that just gets next char until it reaches \0.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And btw, there is no such character as '/0'.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by BC2210 View Post
    Hey,

    In my program to turn an infix statement into a postfix or prefix statement, I want to check if a character from my input is equal to a space OR if it is equal to the end of the array.

    So basically, I have a char array and Im picking off one char at a time, then comparing it.

    So, how do I say:

    if(array[i] == ' ') ?

    and

    if(array[i] == '/0') ?

    I dont think those are correct, but I think thats why my program is messing up.

    Thanks,
    BC
    Code:
    if(array[i] == ' ') ?
    
    *OR*
    
    if(array[i] == '/0') ?

    or more precisely


    Code:
    if ( (array[i] == ' ') || (array[i] == '/0') ) //not tested :O)

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    As Elysia already said, '/0' is not a character.

    Code:
    if(array[i] == ' ' || array[i] == '\0')
    Is suffice.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by zacs7 View Post
    As Elysia already said, '/0' is not a character.

    Code:
    if(array[i] == ' ' || array[i] == '\0')
    Is suffice.
    And if only someone would have posted that before esbo did, he wouldn't have needed to say anything at all.

    Oh wait.

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by tabstop View Post
    And if only someone would have posted that before esbo did, he wouldn't have needed to say anything at all.

    Oh wait.
    I posted because he used the word 'and' which was logically incorrect.
    I gave an example to illustrate that || means 'or' not 'and'.
    I gave up reading the relies half way through reading yours for some reason.
    Anyway if I had not posted you would not have needed to say anything at all :O)
    Last edited by esbo; 11-15-2008 at 07:34 PM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by esbo View Post
    I gave up reading the relies half way through reading yours for some reason.
    Try reading all replies next time. It might, well... Oh, I don't know, help maybe?
    Last edited by Elysia; 11-16-2008 at 04:30 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM