c - 如何在 C 中检查 char 数组的前两个字符?

标签 c function pointers atoi

此代码用于创建类似的 C 库函数 atoi(),而无需使用任何 C 运行时库例程。

我目前陷入如何检查 char 数组 s 的前两位数字以查看输入是否以“0x”开头的问题。

如果它以 0x 开头,这意味着我可以将其转换为十六进制。

#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;
}

最佳答案

您的代码中有几个错误。首先,在 C 语言中,你从零开始计数。所以在 main() 中,你应该这样写:

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

isithex = checkforhex(a, b);

然后,在checkforhex()中,您应该使用==(两个等号)进行比较,而不是=。所以:

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

但是,正如 kaylum 所指出的,为什么不编写函数来传递指向字符串的指针而不是两个字符呢?就像这样:

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

main() 中这样调用它:

isithex = checkforhex(s);

关于c - 如何在 C 中检查 char 数组的前两个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59670550/

相关文章:

c - "Minus"没有被捕获

C++ 可变参数

c++ - 为什么指针没有隐式转换为 <type> const*const

c++ - 当使用 'new int[n]' 声明一个新数组时,为什么它没有出现在本地列表中?

python - 将列表或值传递给 Python 中的函数

c - 如何将堆栈中分配的空指针数组中的元素传输到堆中?

c - 基于字符串参数的 WinDBG 条件断点

c - 指向 C 中结构混淆的指针

c - 函数指针上的 __attribute__

接受函数和列表并返回 bool 值的函数