【问题标题】:How do I check the first two characters of my char array in C?如何在 C 中检查我的 char 数组的前两个字符?
【发布时间】:2020-01-09 19:03:02
【问题描述】:

这是在不使用任何 C 运行时库例程的情况下创建类似 C 库函数 atoi() 的代码。

我目前正纠结于如何检查 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;
}

【问题讨论】:

  • 您的函数被声明为采用指针,而实际参数是普通字符。后者似乎很好(如果数组至少有 2 个字符长),因此将函数中 a 和 b 的声明更改为普通字符。当然,这不会以任何理智的语言编译或链接,即使在 C 中,您也应该收到警告。以高警告级别编译(例如,使用 gcc 的 -Wall 或使用 VC 的 -W4)并注意你的警告。
  • 也就是说,更惯用的方法可能是将单个指针传递给第一个字符并仅在函数内部检查字符串的内容。
  • @Peter-ReinstateMonica 我的输入数组长度可以少于 2 个字符。它可能是空的。我对 C 很陌生,而且我一直在使用 Python 一段时间后,所有带有指针的东西都是如何工作的。如果可以,请编辑代码以显示您想说的内容,这将是惊人的。
  • 在这种情况下,您应该将数组传递给checkforhex 函数。然后在该函数中,您需要检查数组中的第一个字符。如果是\0,则返回 false。如果是0,则继续下一个字符以同样的方式检查x。
  • tolower() 的简短版本:x = x &gt;='A' &amp;&amp; x &lt;='Z' ? x+32 : x;

标签: c function pointers atoi


【解决方案1】:

您的代码中有几个错误。首先,在 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);

【讨论】:

    猜你喜欢
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    相关资源
    最近更新 更多