简体   繁体   中英

Using pointers to subsitute characters in C strings

This is not homework, but study for a midterm.

I cannot use any type of array indexing such as str[i] or *(str+i)

I have to take the c-string "EECS280ISAWESOME" and substitute the 'E' with the c-string "XY". I also have to allow for multiple length of the "XY" variable.

The following main is given:

int main () {
    const char* S = "EECS280ISAWESOME";
    const char* P = "XY";
    char result[256];
    subsituteChar(S,P,'E', result);
    cout << result << endl;
} 

My solution seems complex/bad practice/and ugly. I could do it better with the use of deferencing and adding *(R+1) but I dont think it's allowed.

void subsituteChar(const char* S, const char* P, char c, char* R) {
    while(*S != '\0') {
        if(*S == c) {
            const char* PP = P;
            while (*P != '\0') {
                *R = *P;
                R++;
                P++;
            }
            P = PP;
        } else {
            *R = *S;
            R++;
        }
        S++;
    }
}

This works but I am left with XYXYCS280ISAWXYSOMXY2 . I have no idea where the weird 2 has came from.

Since this is a study problem, here's a hint to get you started. Try initializing your result array. I did this:

char result[256];
for (int i = 0; i < 250; ++i)
    result[i] = 'a';
result[250] = 0;

Now run your code again, and you'll see that you've got lots of 'a' characters at the end of your output, up to the point where you get to character 250. That is, you'll see:

"XYXYCS280ISAWXYSOMXYaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

If you want to figure out the rest for yourself, STOP READING NOW.


The problem is that you're not explicitly null terminating your string.

Note that it's not a problem with your last character of S being replaced, but rather with your while loop's terminal condition. Since you aren't manually copying over the '\\0' character from S, you're left hoping that the result array is full of '\\0' characters, which C and C++ don't guarantee.

Simply adding the following line to the end of your substituteChar function will solve the problem:

*R = '\\0';

Does this fit?

#include<stdio.h>
int main () {
    const char* S = "EECS280ISAWESOME";
    const char* P = "XY";
    char result[256];
    while(*S)
    {
        if(*S=='E') 
            printf("XY");
        else 
            printf("%c", *S);
        S++;
    }
} 

Your code is perfectly correct along with small error. There is a fact of string , everry string should be terminate with null character. so just add the *R ='\\0'; at the end of while function and this algorithm works perfectely.

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