Please comment on my program
- From: "Registered User" <invalidinvalidinvalid@xxxxxxxxx>
- Date: 25 Sep 2006 06:49:54 -0700
I'm a newbie and have attempted to create a program to convert a string
containing the representation of a number in a specified base to a
decimal integer. Here's the code:
#include <stdio.h>
unsigned todecimal( char *s, int base)
{
unsigned num = 0, mul;
int len=0;
while (*++s != '\0')
++len;
--s;
for (mul=1; len>=0; --len, mul*=base, --s)
{
if (*s >= '0' && *s <= '9')
num+= (*s-'0')*mul;
else if (*s>='A'&& *s<='Z')
num+= (*s-'A'+10)*mul;
else if (*s>='a' && *s<='z')
num+=(*s-'a'+10)*mul;
}
return num;
}
int main()
{
char s[33];
int base;
printf("Enter the number and its base: ");
scanf("%s%d",s, &base);
printf("Decimal value = %u\n", todecimal(s, base) );
return 0;
}
.
- Follow-Ups:
- Re: Please comment on my program
- From: dcorbit
- Re: Please comment on my program
- From: Frederick Gotham
- Re: Please comment on my program
- From: Michael Mair
- Re: Please comment on my program
- From: CBFalconer
- Re: Please comment on my program
- Prev by Date: Re: Why does this crash?
- Next by Date: Re: _ and __ significance
- Previous by thread: defining a macro
- Next by thread: Re: Please comment on my program
- Index(es):
Relevant Pages
|
|