isalpha

This is a discussion on isalpha within the C Programming forums, part of the General Programming Boards category; any ideas on what's wrong with this segment? (it compiles in a program, but it doesn't work like it should) ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    isalpha

    any ideas on what's wrong with this segment? (it compiles in a program, but it doesn't work like it should)

    Code:
    #define isalpha_alternate(x) (((x>='a' && x<='z')||(x>='A' && x<='Z')?1:0))

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Is it the braces?
    #define isalpha_alternate(x) (((x>='a' && x<='z')||(x>='A' && x<='Z'))?1:0)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    what are you aiming to acheive?
    isalpha() already determine wether the data is a char,
    if your aim is to print uppercase the char or print to lower case char's

    first test for a char

    main()
    {
    char ch;

    printf("Enter a letter >");
    scanf("%c", &ch);

    if( ch == isalpha(ch))
    {
    toupper(ch);
    //other test
    }
    else
    printf("\nthe letter is not a character");
    }

    maybe!!
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    This works fine for me, be careful what kind of input you pass to the macro and put parens around the variables to ensure it works correctly with strange input.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define isalpha_alternate(x) ((x) >= 'a' && (x) <= 'z'||(x) >= 'A' && (x) <= 'Z') ? 1 : 0
    
    int main(void)
    {
      char ch = 'M';
      printf( "%d\n", isalpha_alternate( 'g' ) );
      printf( "%d\n", isalpha_alternate( ch ) );
      printf( "%d\n", isalpha_alternate( '7' ) );
    
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome, problem with isalpha
    By Kyeong in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 01:28 PM
  2. Problem with isalpha
    By kron_19792000 in forum C Programming
    Replies: 3
    Last Post: 09-26-2005, 01:13 AM
  3. isalpha || ispunct
    By linuxdude in forum C Programming
    Replies: 3
    Last Post: 05-21-2004, 10:43 PM
  4. isalpha
    By mackol in forum C Programming
    Replies: 9
    Last Post: 11-29-2002, 02:55 PM
  5. Floating Point isalpha();
    By UnclePunker in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21