user11312051
user11312051

Reputation:

How do I check the first two characters of my char array in C?

This is code to create a similar C library function atoi() without the use of any C runtime library routines.

I'm currently stuck on how to check for the first two digits of the char array s to see whether the input begins with "0x".

If it starts with 0x, this means that I can then convert it in to hexadecimal.

#include <stdio.h>

int checkforint(char x){
    if (x>='0' && x<='9'){
        return 1;
    }
    else{
        return 0;
    }
}

unsigned char converthex(char x){
    //lets convert the character to lowercase, in the event its in uppercase
    x = tolower(x);
    if (x >= '0' && x<= '9') {
        return ( x -'0');
    }
    if (x>='a' && x<='f'){
        return (x - 'a' +10);
    }
    return 16;
}

int checkforhex(const char *a, const char *b){
    if(a = '0' && b = 'x'){
        return 1;
    }else{
        return 0;
    }
}

//int checkforint
/* We read an ASCII character s and return the integer it represents*/
int atoi_ex(const char*s, int ishex) 
{
    int result = 0; //this is the result
    int sign = 1; //this variable is to help us deal with negative numbers
                //we initialise the sign as 1, as we will assume the input is positive, and change the sign accordingly if not

    int i = 0; //iterative variable for the loop
    int j = 2;

    //we check if the input is a negative number
    if (s[0] == '-') {   //if the first digit is a negative symbol 
        sign = -1;      //we set the sign as negative
        i++;            //also increment i, so that we can skip past the sign when we start the for loop
    }

    //now we can check whether the first characters start with 0x

    if (ishex==1){
        for (j=2; s[j]!='\0'; ++j)
            result = result + converthex(s[j]);
        return sign*result;
    }

    //iterate through all the characters 
    //we start from the first character of the input and then iterate through the whole input string
    //for every iteration, we update the result accordingly

    for (; s[i]!='\0'; ++i){
        //this checks whether the current character is an integer or not
        //if it is not an integer, we skip past it and go to the top of the loop and move to the next character
        if (checkforint(s[i]) == 0){
            continue;
        } else {
            result = result * 10 + s[i] -'0';
        }
        //result = s[i];
        }

    return sign * result;
}

int main(int argc)
{   
    int isithex;

    char s[] = "-1";
    char a = s[1];
    char b = s[2];

    isithex=checkforhex(a,b);

    int val = atoi_ex(s,isithex);
    printf("%d\n", val);
    return 0;
}

Upvotes: 1

Views: 2402

Answers (1)

G. Sliepen
G. Sliepen

Reputation: 7973

There are several errors in your code. First, in C you start counting from zero. So in main(), you should write:

char a = s[0];
char b = s[1];

isithex = checkforhex(a, b);

Then, in checkforhex(), you should use == (two equal signs) to do comparisons, not =. So:

if (a == '0' && b == 'x')

However, as pointed out by kaylum, why not write the function to pass a pointer to the string instead of two characters? Like so:

int checkforhex(const char *str) {
    if (str[0] == '0' && str[1] == 'x') {
        ...
    }
}

And in main() call it like so:

isithex = checkforhex(s);

Upvotes: 1

Related Questions