By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,666 Members | 1,909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,666 IT Pros & Developers. It's quick & easy.

declaring alpha numeric

P: n/a
how to declare a variable that should contain both alpahabets and
numbers.It should not be an string or char array.

Nov 16 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
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
Nov 16 '05 #2

P: n/a

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

Nov 16 '05 #3

P: n/a
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.
Nov 16 '05 #4

P: n/a
My question is the variable should hold items like mn995# .

Nov 16 '05 #5

P: n/a

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

Nov 16 '05 #6

P: n/a

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?

Nov 16 '05 #7

P: n/a
"shan" <sr**********@gmail.com> 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) ks***@mib.org <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.
Nov 16 '05 #8

P: n/a
"shan" <sr**********@gmail.com> 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
Nov 16 '05 #9

P: n/a
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
Nov 16 '05 #10

P: n/a
In article <11**********************@g47g2000cwa.googlegroups .com>,
shan <sr**********@gmail.com> 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

Nov 16 '05 #11

P: n/a
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.
Nov 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.