Thread: need advise on if statements

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22

    need advise on if statements

    Hi! I would like to seek advice or insights regarding this lengthy program that I made for an assignment and was wodering if it can be shortened or if there is something wrong with it, although it works fine.

    Here's the code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int main(void)
    {
       char name[20];
    
       printf("Enter Lastname Initial: ");
       scanf("%s", name);
    
       if(name[0]=='a' || name[0]=='c' || name[0]=='b' || name[0]=='d' || name[0]=='e' || name[0]=='f' || name[0]=='g' || name[0]=='h' || name[0]=='i' || name[0]=='j' || name[0]=='k' || name[0]=='l' || name[0]=='m')
        {
           printf("\nLine # 2");
        }
    
        if(name[0]=='n' || name[0]=='o' || name[0]=='p' || name[0]=='q' || name[0]=='r' || name[0]=='s' || name[0]=='t' || name[0]=='u' || name[0]=='v' || name[0]=='w' || name[0]=='x' || name[0]=='y' || name[0]=='z')
        {
            printf("\nLine # 1");
        }
    
        getche();
    }
    Thanks for the help!! ^^

  2. #2
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    Quote Originally Posted by jannr View Post
    Hi! I would like to seek advice or insights regarding this lengthy program that I made for an assignment and was wodering if it can be shortened or if there is something wrong with it, although it works fine.

    Here's the code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    int main(void)
    {
       char name[20];
    
       printf("Enter Lastname Initial: ");
       scanf("%s", name);
    
       if(name[0]=='a' || name[0]=='c' || name[0]=='b' || name[0]=='d' || name[0]=='e' || name[0]=='f' || name[0]=='g' || name[0]=='h' || name[0]=='i' || name[0]=='j' || name[0]=='k' || name[0]=='l' || name[0]=='m')
        {
           printf("\nLine # 2");
        }
    
        if(name[0]=='n' || name[0]=='o' || name[0]=='p' || name[0]=='q' || name[0]=='r' || name[0]=='s' || name[0]=='t' || name[0]=='u' || name[0]=='v' || name[0]=='w' || name[0]=='x' || name[0]=='y' || name[0]=='z')
        {
            printf("\nLine # 1");
        }
    
        getche();
    }
    Thanks for the help!! ^^
    instead of checking first with each alphabet try...
    Code:
    if(name[0]<'n')
        {
           printf("\nLine # 2");
        }
        else
        {
            printf("\nLine # 1");
        }

  3. #3
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    Quote Originally Posted by subhash.rao View Post
    instead of checking first with each alphabet try...
    Code:
    if(name[0]<'n')
        {
           printf("\nLine # 2");
        }
        else
        {
            printf("\nLine # 1");
        }
    ahh,, It works!! Thanks! I thought that c won't recognize alphabets in counting. lol,, so here's another question, do this run on turbo c?

  4. #4
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    ya...it should work...but if some one enters a digit for the first place it will print line # 2...

  5. #5
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    Quote Originally Posted by subhash.rao View Post
    ya...it should work...but if some one enters a digit for the first place it will print line # 2...
    Thanks! then I'll just have to instruct the user only to enter alphabets,, ^^

    Thanks agen subhash.rao,, ^^

  6. #6
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    u can use isaplha function present in ctype.h to check whether the entered value is a alphabet or not......

  7. #7
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    Thanks!!

  8. #8
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    Hi again. what's wrong with this code?
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
       char name[20];
    
       printf("Enter Lastname Initial: ");
       scanf("%s", name);
    
       if(name[0]<'N')
        {
           printf("\nLine # 2");
        }
    
       if(name[0]>='N')
        {
            printf("\nLine # 1");
        }
    
    
        getche();
    }
    when I input the small letter m it prints Line # 1 rather than Line # 2,,
    Ps. I'm using if's only because we haven't discussed else yet,,

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    when I input the small letter m it prints Line # 1 rather than Line # 2
    That's because, according to the ASCII chart, small 'm' is greater than big 'N'.

    You can always expand your current "if()" statements to take upper/lowercase into account:

    Code:
    // warning; code untested, for reference only
    
    if((name[0]>='A' && name[0]<='M') || (name[0]>='a' && name[0]<='m'))
        {
           printf("\nLine # 2");
        }
    
    if((name[0]>='N' && name[0]<='Z') || (name[0]>='n' && name[0]<='z'))
        {
            printf("\nLine # 1");
        }
    Last edited by Matticus; 07-18-2011 at 04:05 AM.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    Quote Originally Posted by Matticus View Post
    That's because, according to the ASCII chart, small 'm' is greater than big 'N'.

    You can always expand your current "if()" statements to take upper/lowercase into account:

    Code:
    // warning; code untested, for reference only
    
    if((name[0]>='A' && name[0]<='M') || (name[0]>='a' && name[0]<='m'))
        {
           printf("\nLine # 2");
        }
    
    if((name[0]>='N' && name[0]<='Z') || (name[0]>='n' && name[0]<='z'))
        {
            printf("\nLine # 1");
        }
    Thanks Matticus! but why is it like that?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Every character has a unique numeric code, as per the link to the ASCII table I provided. The characters that represent uppercase letters occur earlier in the table than the characters that represent lowercase letters.

    If we study the table, we see that:

    - 'A' has a decimal value of 65
    - 'M' has a decimal value of 77
    - 'N' has a decimal value of 78
    - 'Z' has a decimal value of 90

    - 'a' has a decimal value of 97
    - 'm' has a decimal value of 109
    - 'n' has a decimal value of 110
    - 'z' has a decimal value of 122

    So therefore,

    Code:
    if((name[0]>='A' && name[0]<='M') || (name[0]>='a' && name[0]<='m'))
    is equivalent to,

    Code:
    if((name[0]>=65 && name[0]<=77) || (name[0]>=97 && name[0]<=109))
    Which basically says "if the variable is in the range of uppercase 'A' to uppercase 'M', or in the range of lowercase 'a' to lowercase 'm', then print out message."

  12. #12
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    ahh,, that's right,, thanks a lot Matticus,, that clears things up,, ^^

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    C doesn't depend on ASCII, there are other encoding sets -> Extended Binary Coded Decimal Interchange Code - Wikipedia, the free encyclopedia
    EBCDIC has neither 'A' before 'a', nor a contiguous sequence for all letters.

    A portable and concise test would be
    Code:
    if ( strchr( "ABCDEF", toupper(name[0]) ) != NULL ) {
      // do stuff
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jannr View Post
    Hi again. what's wrong with this code?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
       char name[20];
    
       printf("Enter Lastname Initial: ");
       scanf("%s", name);
    
       if(toupper(name[0])<'N')
        {
           printf("\nLine # 2");
        }
       else 
        {
            printf("\nLine # 1");
        }
    
    
        getchar();
    
       return 0;
    }
    Last edited by CommonTater; 07-18-2011 at 07:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advise for the day.
    By Mario F. in forum General Discussions
    Replies: 8
    Last Post: 04-01-2010, 03:51 PM
  2. please advise....
    By manny in forum C Programming
    Replies: 2
    Last Post: 06-07-2006, 06:09 AM
  3. Need some advise
    By ILoveVectors in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2005, 09:24 AM
  4. New student asking for advise
    By Tier in forum C Programming
    Replies: 5
    Last Post: 09-17-2003, 10:23 AM
  5. Some Advise..
    By VaSt in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2003, 01:52 PM