在C中,检查字符串中的偏移量并计算字节数

发布于 2025-01-04 12:15:46 字数 650 浏览 1 评论 0原文

对我之前询问的作业的进一步帮助。从 stdin 读取字符串后,它会检查是否有任何非 ASCII 字符。它找到的任何内容都将以十六进制显示其值和零偏移量。我可以很好地显示十六进制,但我很难弄清楚如何显示偏移量。这是我在提出这个问题时当前的代码,仅显示相关行。

#define MASK 0x80

auto int inChar;

if ((inChar & MASK) == MASK)
{
    printf("NON-ASCII INPUT: %x detected at offset %#x \n", inChar, inChar);
    nonascii = 1;
}

还有一件事我需要帮助,希望这会更容易回答。如果读取的行只有 ASCII,则按原样写出并显示读取的总字节数。我知道如何通过简单地计算 do while 完成的次数来使用整数和乘法来做到这一点,但教授似乎希望我们以更直接的方式做到这一点。

这是我撰写本文时的代码。

if (nonascii == 0)
    printf("The input stream was pure ASCII with a total of %d bytes read", (numBytes &= inChar));

非常感谢那些在这方面帮助我的人。

Further help on my assignment I asked about earlier. After reading a character string from stdin, it checks for any non-ascii characters. Any it finds will display it's value in hexadecimal and the zero offset. I got it to display the hexadecimal just fine, but I'm having a hard time figuring out how to display the offset. This is my current code for it as of asking this question, showing only the relevant lines.

#define MASK 0x80

auto int inChar;

if ((inChar & MASK) == MASK)
{
    printf("NON-ASCII INPUT: %x detected at offset %#x \n", inChar, inChar);
    nonascii = 1;
}

There is another thing I need help with, hopefully this will be easier to answer though. If the line read has nothing but ASCII, it is to write out as such and display the total number of bytes read. I know how to do this with integers and multiplication by simply counting the number of times a do while is done, but the professor seems to want us to do it in a more direct manner.

This is my code for that as of writing.

if (nonascii == 0)
    printf("The input stream was pure ASCII with a total of %d bytes read", (numBytes &= inChar));

Much thanks to those who help me with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦屿孤独相伴 2025-01-11 12:15:46

inChar 包含读取的字符的;它不携带有关字符在字符串中的位置的信息,因此无法计算出该变量的偏移量。

看的地方就是你用当前字符填充inChar的地方;不幸的是,我们看不到这部分代码。最有可能的是,此时您正在从字符串中的某个索引中提取值,并且您应该只使用相同的索引变量来显示偏移量。另一种可能性是您通过在字符串中移动指针来从字符串中提取值。在这种情况下,提取偏移量的方法是使用指针算术:假设您有指向字符串开头的指针stringstringIter< /strong> 指向当前处理的字符,然后查看指针彼此之间的距离将告诉您字符串中的偏移量:stringIter - string

inChar contains the value of the character read; it does not carry information about where in the string the character is, therefore it is not possible to figure out the offset from this variable.

The place to look at is wherever you fill inChar with the current character; unfortunately, we do not see that part of the code. Most likely, at that point, you are extracting the value from a certain index in the string and you should just use the same index variable for displaying the offset. Another possibility is that you are extracting the value from the string by moving a pointer within the string. In that case, the way to extract the offset is by using pointer arithmetic: say that you have pointer string that points to the beginning of the string and stringIter that points at the currently processed character, then looking at how far the pointers are from each other will tell you your offset in the string: stringIter - string.

想挽留 2025-01-11 12:15:46

inChar 是一个字符,而不是偏移量。偏移量表示在字符串中找到字符的数组索引,您可能应该使用 %d 格式化程序将其打印为十进制。

显示字符串长度的最简单方法是使用 strlen() 函数。但如果你不允许使用它,那么当你跳出循环时,字符串的长度就是数组索引+1。

inChar is a character, not an offset. Offset means the array index of where the character was found in the string, and you should probably print it as decimal using the %d formatter.

The simplest way to display a string's length is with the strlen() function. But if you're not allowed to use that, then the length of the string is the array index + 1 when you fall out of the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文