Thread: Comparing characters.

  1. #1

    Comparing characters.

    I'm trying to get a program to run where I need to take in input and show whether it's part of the alphabet or not (a - z, A-Z ). When I try to put in a command like this..

    char a;
    if (a >= "a" || a <= "z")

    , it gives me a bunch or errors. What do I need to do to get it to recognize letters in the alphabet?

    Thanks in advance.

  2. #2
    well...start with single quotes instead of double. ", '

  3. #3
    Ah, that made it so I can compile. But it's still reading in semicolons and printing them out though I said for it to read from 'a' to 'Z'.

  4. #4
    Registered User
    Join Date
    Aug 2001

    if (a >= 'a' || a <= 'z')


    if (a >= 'a' && a <= 'z')

  5. #5
    Registered User
    Join Date
    Nov 2001
    To include capitals as well...

    if ((a >= 'a' && a <= 'z')||(a >= 'A' && a <= 'Z'))



    mmmmmmmm... iterations

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    char Alphabet []={A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X, Y,Z,
    a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y, z};

    char TestCharacter='A';

    char *ptr=strchr(Alphabet,TestCharacter);

    if (ptr)
    //Character is valid
    //Character is invalid

    You can also check the ASCII value of the input character. If it is in range (65-90 for capitals and 97-122 for lower case) then it is
    part of the alphabet.

    int hit=kbhit();
    int ch=0;

    if (hit)

    if (ch>=65 && ch<=90 || ch>=97 && ch<=122)
    //Character is in the alphabet

    kbhit() would be in a loop constantly checking to see if a key had been hit. If you do not wish to check for both ranges, simply convert the character to upper case or lower case with strlwr() or strupr() and compare to the upper or lower case ASCII codes respectively. This is the same as if (ch>'A'....) since 'A' represents the number 65.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Comparing characters
    By Banana Man in forum C++ Programming
    Replies: 28
    Last Post: 01-13-2008, 11:11 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Comparing Characters
    By luckygold6 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2003, 08:19 PM