Exercises 3-5

#include <stdio.h>
void itob(int n,char s[],int b);
void reverse(char s[]);
int main(int argc, char *argv[])
{
    int n=1234;
    char s[64];
    itob(n,s,16);
    printf("%s\n",s);
    return 0;
}
void itob(int n,char s[],int b)
{

    int sign,d,i;
    i=0;
    if((sign=n)<0)        //if n is a negative
        n=-n;
    do
    {
        d=n%b;
        s[i++]=(d<=9)?d+'0':d-10+'a';
    }while((n/=b)>0);
    if(sign<0)
        s[i++]='-';
    s[i]='\0';
    reverse(s);
}

void reverse(char s[])
{
    int i=0,j;
    int len=0;
    while(s[i++]!='\0')
        len++;
    if(len>1)
    {
        char c;
        j=len-1;
        i=0;
        while(i<j)
        {
            c=s[i];
            s[i]=s[j];
            s[j]=c;
            i++;
            j--;
        }
    }


}


你可能感兴趣的:(exe)