Thread: if and while conditions

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    20

    Question if and while conditions

    I'm trying to parse a string (polynomial function) for my program. Are these conditions allowed for if and while?

    if (a >= '0' && a <= '9') ---> checks if char is a number
    if ( a>= 'A' && a <= 'Z') ---> checks if char is a letter
    if (a >= 'a' && a <= 'z')


    while(r[i] != '\0') ---> repetition for all the characters.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes, they're allowed. The quick way to find out is to test them...

    The only thing is you may want to add a couple of extra brackets just to gurantee the behaviour you want...

    Code:
    if (( a >= '0' ) && ( a <= '9' )) 
      { //whatever }
    This guarantees the tests on a are completed before the && condition is tried instead of left to right evaluation which might give a different answer...

    Also you could use the isdigit() and isalpha() functions from the std library for this.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    oh thanks! I tried it and it worked. A little problem with the condition for numbers though.
    When using this sample code, the numbers in the string passes the 2nd if, and also the else.

    while(eq[i] != '\0') {
    if(isNum(eq[i])) {printf("%c - number\n", eq[i]);}
    if(isAlpha(eq[i])) {printf("%c - letter\n", eq[i]);}
    else{ printf("%c - etc\n");}
    i++;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I assume that "isNum" is something you wrote, same with "isAlpha"? There is no capital letter in the naming of any of the standard is* functions.

    Also, use CODE TAGS. I don't know how the hell you manage to keep getting braces past the filter.


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

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by CommonTater View Post
    Also you could use the isdigit() and isalpha() functions from the std library for this.
    I tried the isdigit and isalpha with the stdlib header file but the compiler says it's undeclared.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by firehydrant View Post
    oh thanks! I tried it and it worked. A little problem with the condition for numbers though.
    When using this sample code, the numbers in the string passes the 2nd if, and also the else.

    while(eq[i] != '\0') {
    if(isNum(eq[i])) {printf("%c - number\n", eq[i]);}
    if(isAlpha(eq[i])) {printf("%c - letter\n", eq[i]);}
    else{ printf("%c - etc\n");}
    i++;
    }
    Ok frst thing ... code tags. Please learn how to use them... << !! Posting Code? Read this First !! >> ... makes life a whole lot easier.

    Second the setup of your code does not clearly show it's function... it may look ok now but will you still be able to follow it when you come back to it in a year?

    Code:
    while( eq[i] != '\0 ') {
       if( isNum( eq[i] ) ) 
           printf( "%c  - number\n", eq[i] );
       else if( isAlpha( eq[i] ) ) 
          printf( "%c  - letter\n", eq[i] );
       else
         printf( "%c  - etc\n"[b], eq[i] );
       i++;
    }
    The others will probably tell you I'm the last one to lecture on code formatting since in my own work I use a very unusual style but I'm sure you can see how much more readable your code is when it's set up nicely.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by firehydrant View Post
    I tried the isdigit and isalpha with the stdlib header file but the compiler says it's undeclared.
    They aren't in stdlib, they are in ctype.h. You do know that you're on the internet, right? You are allowed to search it for things.


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

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by firehydrant View Post
    I tried the isdigit and isalpha with the stdlib header file but the compiler says it's undeclared.
    Sorry I meant the standard [set of] libraries... My bad.

    They're in ctype.h

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    @quzah

    isNum and isAlpha are functions I wrote in the program.
    I'm new in this forum so sorry for that.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by firehydrant View Post
    @quzah

    isNum and isAlpha are functions I wrote in the program.
    I'm new in this forum so sorry for that.
    They're also part of standard C99 ... while re-writing them might be an interesting exercise, there's not much point when they're already provided for you.

    http://en.wikipedia.org/wiki/Ctype.h

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    this is already a bit off topic, but it's still part of the program i'm working on. I saw this sample code about parsing.

    Code:
    /* strtok example */
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
      char str[] ="- This, a sample string.";
      char * pch;
      printf ("Splitting string \"%s\" into tokens:\n",str);
      pch = strtok (str," ,.-");
      while (pch != NULL)
      {
        printf ("%s\n",pch);
        pch = strtok (NULL, " ,.-");
      }
      return 0;
    }
    Sample code is not mine.
    I got it here: http://www.cplusplus.com/reference/clibrary/cstring/strtok/

    How do you store the tokens, say in an array, after parsing? Or is it already stored somewhere?

  12. #12
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    You have gotten good advice. I want to add one more comment.

    Quote Originally Posted by firehydrant View Post
    if ( a>= 'A' && a <= 'Z') ---> checks if char is a letter
    if (a >= 'a' && a <= 'z')
    The uppercase letters are not guaranteed to be contiguous. The reason the language standard did not require them to be is because there are C implementations designed for systems that use EBCDIC as either their source code and/or execution character set. In EBCDIC, the letters are not contiguous. They are in ASCII and most (all?) of the character sets based on them.

    This is one of the advantages of using the isalpha(), isupper(), and islower() functions or macros from the standard library (mentioned ealier in the thread). They will deal with these changes in character sets in a portable way. This is only important if you need or want to be portable.

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by pheininger View Post
    You have gotten good advice. I want to add one more comment.



    The uppercase letters are not guaranteed to be contiguous. The reason the language standard did not require them to be is because there are C implementations designed for systems that use EBCDIC as either their source code and/or execution character set. In EBCDIC, the letters are not contiguous. They are in ASCII and most (all?) of the character sets based on them.

    This is one of the advantages of using the isalpha(), isupper(), and islower() functions or macros from the standard library (mentioned ealier in the thread). They will deal with these changes in character sets in a portable way. This is only important if you need or want to be portable.
    Thanks! I'll try the isupper and islower later.

Popular pages Recent additions subscribe to a feed