简体   繁体   English

如何标记C字符串的结尾?

[英]How can I mark the end of a C string?

Here is the code for the function: 这是该函数的代码:

void pattern(char word[200], char asPattern[200]) {
    for (unsigned i = 0; i < strlen(word); i++) { // unsigned to remove warning
        if (strchr("aeiou", word[i]) != 0) asPattern[i] = '*';
        else asPattern[i] = '#';
}

Basically this function replaces consonants in word with #'s and vowels with *'s, and stores the new pattern in the asPattern string. 基本上,此函数将单词中的辅音替换为#,将元音替换为*,并将新模式存储在asPattern字符串中。 However, if I display the asPattern on the screen, it shows the correct pattern followed by a bunch of unknown symbols (strlen(asPattern) equals 211 or something after the for loop). 但是,如果我在屏幕上显示asPattern,它将显示正确的模式,后跟一堆未知符号(strlen(asPattern)等于211或for循环后的值)。 I believe the problem is that the asPattern's end isn't marked and asPattern[strlen(asPattern)] = '/0' doesn't work and I don't know what else to do... 我认为问题在于,未标记asPattern的结尾,并且asPattern [strlen(asPattern)] ='/ 0'无效,我不知道该怎么办...

I cannot use the std::string, so please bear with me and use C strings. 我不能使用std :: string,所以请忍受并使用C字符串。

add the code 添加代码

asPattern[strlen(word)] = '\0';

either before or after the for-loop for循环之前或之后

C-strings are terminated by null character, which can be represented by literal 0 or '\\0'. C字符串以空字符终止,可以用文字0或'\\ 0'表示。 Functions that work with C-strings (like strlen) expects that. 使用C字符串的函数(如strlen)期望得到这种效果。 Note that strlen() scans string for null character, so your loop: 请注意, strlen()扫描字符串中的空字符,因此您的循环如下:

for (unsigned i = 0; i < strlen(word); i++)

is ineffective - strlen() has to scan word on every iteration. 是无效的strlen()必须在每次迭代中扫描word So better code could be: 因此更好的代码可能是:

void pattern(char word[200], char asPattern[200]) {
    const unsigned len = strlen(word);
    for (unsigned i = 0; i < len; i++) { // unsigned to remove warning
        if (strchr("aeiou", word[i]) != 0) asPattern[i] = '*';
        else asPattern[i] = '#';
    }
    asPattern[len] = 0; // or '\0' if you prefer
}

The simplest, almost foolproof solution is just set the entire block of characters to \\0 . 最简单,几乎万无一失的解决方案是将整个字符块设置为\\0 This works correctly, assuming you have prior knowledge of the actual size of the char array. 假设您已预先了解char数组的实际大小,则此方法可以正常工作。

#include <string.h>
void pattern(char word[200], char asPattern[200]) 
{
    memset(asPattern, '\0', 200);  // we are assuming that there really are 200  bytes
    //...
}

Once you do this, then you need not worry about the null terminator. 完成此操作后,就不必担心空终止符了。

Also, note that this: 另外,请注意:

void pattern(char word[200], char asPattern[200]) 

is no different than this: 没什么不同:

void pattern(char* word, char* asPattern) 

So actually, that function doesn't really know how big asPattern is. 所以实际上,该函数并不真正知道asPattern有多大。 So again, if you know up front what the size is, and you know that you will be overwriting the string anyway, just use memset and forget the headaches of whether you should be using strlen() or whatever other scheme there might be to figure out where the null byte goes. 再说一次,如果您预先知道大小是多少,并且知道无论如何都会覆盖该字符串,则只需使用memset ,而不必担心是否应该使用strlen()或任何其他方案来计算空字节的去向。

I have tested your function and it works fine. 我已经测试了您的功能,并且工作正常。 (here is the snippet: http://rextester.com/XDSU30889 ) (以下是代码段: http : //rextester.com/XDSU30889

the "asPattern" must be sent to the function cleared from any content (it must be full with '\\0' -s, that's what the GlobalAlloc() does if you're on Windows, otherwise use malloc() then memset() 必须将“ asPattern”发送到从任何内容中清除的函数(它必须充满'\\0' 0'-s,如果您在Windows上,这就是GlobalAlloc()功能,否则请使用malloc()然后memset()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM