declaring alpha numeric

S

shan

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

Skarmander

shan said:
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.
 
D

David Resnick

shan said:
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
 
P

pete

shan said:
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.
 
D

David Resnick

shan said:
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
 
M

Michel Rouzic

David said:
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?
 
K

Keith Thompson

shan said:
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?
 
S

SM Ryan

# 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).
 
E

Emmanuel Delahaye

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.
 
R

Richard Tobin

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
 
S

Simon Biber

shan said:
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 */
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top