简体   繁体   中英

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. 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). 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...

I cannot use the std::string, so please bear with me and use C strings.

add the code

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

either before or after the for-loop

C-strings are terminated by null character, which can be represented by literal 0 or '\\0'. Functions that work with C-strings (like strlen) expects that. Note that strlen() scans string for null character, so your loop:

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

is ineffective - strlen() has to scan word on every iteration. 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 . This works correctly, assuming you have prior knowledge of the actual size of the char array.

#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. 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.

I have tested your function and it works fine. (here is the snippet: 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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