declaring alpha numeric

Discussion in 'C Programming' started by shan, Nov 16, 2005.

  1. shan

    shan Guest

    how to declare a variable that should contain both alpahabets and
    numbers.It should not be an string or char array.
     
    shan, Nov 16, 2005
    #1
    1. Advertising

  2. shan

    Skarmander Guest

    shan wrote:
    > how to declare a variable that should contain both alpahabets and
    > numbers.It should not be an string or char array.
    >

    int i;
    i = 'a';
    i = '0';

    S.
     
    Skarmander, Nov 16, 2005
    #2
    1. Advertising

  3. shan wrote:
    > how to declare a variable that should contain both alpahabets and
    > numbers.It should not be an string or char array.


    Your question is not very clear. Do you mean like this?

    int foo = 'a';

    foo = 123;

    The integer variable foo can be used to hold a letter or a number, is
    that
    what you are after?

    -David
     
    David Resnick, Nov 16, 2005
    #3
  4. shan

    pete Guest

    shan wrote:
    >
    > how to declare a variable that should contain both alpahabets and
    > numbers.It should not be an string or char array.


    A structure can hold various types simultaneously.

    --
    pete
     
    pete, Nov 16, 2005
    #4
  5. shan

    shan Guest

    My question is the variable should hold items like mn995# .
     
    shan, Nov 16, 2005
    #5
  6. shan wrote:
    > My question is the variable should hold items like mn995# .


    Two things.

    1) That looks much like a char array is what is wanted.
    Why don't you want to use one? Explain the use to which
    you intend to put the variable and why a char array doesn't
    meet your needs, and perhaps we can help more? Is this
    homework?

    2) When replying, please quote stuff so everyone can see the context.
    To quote another poster:
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson

    -David
     
    David Resnick, Nov 16, 2005
    #6
  7. David Resnick wrote:
    > shan wrote:
    > > My question is the variable should hold items like mn995# .

    >
    > Two things.
    >
    > 1) That looks much like a char array is what is wanted.
    > Why don't you want to use one? Explain the use to which
    > you intend to put the variable and why a char array doesn't
    > meet your needs, and perhaps we can help more? Is this
    > homework?


    Maybe he wants something like a structure holding two chars, one int
    and one char?
     
    Michel Rouzic, Nov 16, 2005
    #7
  8. "shan" <> writes:
    > how to declare a variable that should contain both alpahabets and
    > numbers.It should not be an string or char array.


    We don't understand the question. You're misusing the word
    "alphabets"; what exactly do you mean? There are several kinds of
    numbers (integer and floating-point). Do you want a variable that can
    hold different things simultaneously? Why should it not be a string
    or char array?

    Questions of the form "How can I do X without using language feature Y?"
    are often homework (in real life, you're free to use whatever language
    feature will get the job done). Is this a homework assignment?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Nov 16, 2005
    #8
  9. shan

    SM Ryan Guest

    "shan" <> wrote:
    # how to declare a variable that should contain both alpahabets and
    # numbers.It should not be an string or char array.

    C doesn't have anything like a Cobol PICTURE (A).

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    TEMPORARILY CLOSED
    BE OPENED AFTER FIRST PERIOD
     
    SM Ryan, Nov 16, 2005
    #9
  10. shan a écrit :
    > My question is the variable should hold items like mn995# .
    >

    This is nothing but a string.

    If you want to give a semantic to the subfields, it's up to you. a
    structure can hold it.

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Nov 16, 2005
    #10
  11. In article <>,
    shan <> wrote:
    >how to declare a variable that should contain both alpahabets and
    >numbers.It should not be an string or char array.


    If you want a variable that can contain either numbers or strings,
    you could use a union, but you will have to keep track of which it
    contains.

    If you mean (contrary to what you say) that you want a string that
    is restricted to alphanumeric characters, there's no way to do that
    in C; you'll have to check it yourself.

    -- Richard
     
    Richard Tobin, Nov 16, 2005
    #11
  12. shan

    Simon Biber Guest

    shan wrote:
    > how to declare a variable that should contain both alpahabets and
    > numbers.It should not be an string or char array.


    Here I give you a program in three files. The first is main.c, which
    demonstrates the use of the alphanumeric type. The second is
    alphanumeric.h, which is a header file that you must include to have
    access to the alphanumeric type and the functions that use it. The third
    file contains the implementation of the functions.

    /* main.c */

    #include "alphanumeric.h"

    #include <stdio.h>

    int main(void)
    {
    alphanumeric *p = new_alphanumeric("mn955");
    if(!p)
    {
    printf("No, that was not alphanumeric!\n");
    return 0;
    }
    print_alphanumeric(p);
    delete_alphanumeric(p);
    putchar('\n');
    return 0;
    }


    /* alphanumeric.h */

    #ifndef H_ALPHANUMERIC
    #define H_ALPHANUMERIC

    typedef struct alphanumeric alphanumeric;

    alphanumeric *new_alphanumeric(const char *);
    void print_alphanumeric(const alphanumeric *);
    void delete_alphanumeric(alphanumeric *);

    #endif


    /* alphanumeric.c */

    #include "alphanumeric.h"

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    struct alphanumeric
    {
    char *data;
    };

    static const char *alnum = "0123456789"
    "abcdefghijklmnopqrstuvwxyz"
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    alphanumeric *new_alphanumeric(const char *s)
    {
    size_t n = strlen(s);
    alphanumeric *new;
    if(strspn(s, alnum) != n)
    {
    return NULL;
    }
    new = malloc(sizeof *new);
    if(!new)
    {
    return NULL;
    }
    new->data = malloc(n + 1);
    if(!new->data)
    {
    free(new);
    return NULL;
    }
    memcpy(new->data, s, n + 1);
    return new;
    }

    void print_alphanumeric(const alphanumeric *a)
    {
    printf("%s", a->data);
    }

    void delete_alphanumeric(alphanumeric *a)
    {
    free(a->data);
    free(a);
    }

    /* end of alphanumeric.c */

    --
    Simon.
     
    Simon Biber, Nov 17, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Saunders

    Re: alpha numeric sorting for dataview

    John Saunders, Jul 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,718
    John Saunders
    Jul 20, 2004
  2. Replies:
    5
    Views:
    6,122
    Daniele Futtorovic
    Aug 19, 2008
  3. WB

    ResetPassword() to Alpha-numeric?

    WB, Jul 25, 2007, in forum: ASP .Net Security
    Replies:
    3
    Views:
    609
    Christopher Johnson
    Feb 22, 2010
  4. Replies:
    1
    Views:
    183
    Robert Klemme
    May 31, 2006
  5. John Smith
    Replies:
    8
    Views:
    180
    David K. Wall
    Sep 26, 2003
Loading...

Share This Page