简体   繁体   中英

Replacing characters in a char array c++

i have a char array that is filled with 1s and 0s. If there is a 0 after a 1, then they need to switch places. The cycle repeats k times. I wrote this code, but when i try to print the string, it doesn't print out anything or it just prints out a 1. Why does it do this and why doesn't it work?

#include <iostream>
using namespace std;
int const limit = 100000;

int main() {
    int k;
    char s[limit];

    cin >> k;
    cin >> s;

    for (int i = 0; i < k; i++) {
        for (int j = 0; j < strlen(s); j++)
            if (s[j + 1] == '0' && s[j] == '1') {
                s[j + 1] = '1';
                s[j] = 0;
                }
            }
    }

    cout << s;

    return 0;
}

a) There is an erroneous curly brace.

b) You need to change s[j] = 0 to s[j] = '0' .

c) Although not an error, for clarification I would add curly braces around the second for loop. This makes the code easier to read.

#include <cstring>
#include <iostream>

using namespace std;
int const limit = 100000;

int main() {
    int k;
    char s[limit];

    cin >> k;
    cin >> s;

    for (int i = 0; i < k; i++) {
        for (int j = 0; j < strlen(s); j++) {
            if (s[j + 1] == '0' && s[j] == '1') {
                s[j + 1] = '1';
                s[j] = '0';
            }
        }
    }

    cout << s;

    return 0;
}

I would first start with the understanding of the statement above: int const limit = 100000; means (as you already know) you have assigned to your named variable "limit", a constant integer value of 100000. The value 100000 is a single numerical value integer; not an ASCII character array of '0's and '1's (if I am following your thinking correctly). The numerical value of 100000 is assigned a single location in storage and cannot be changed in the code; unless you change the code yourself to another value (ie, int const limit = 15;). (Does not make sense).

// My thinking is your trying to do something like this maybe?

#include <iostream>
#include <string>

int(main) {
int k = 10;

char s[] = "0 1 0 1 0 1 1 0 1 1":  // let the compiler count

// create a nested for loop to change the '0' char to a '1' char here as you
// want to do above.
.... code ....

return 0;
}

You say you have a char array, s[limit], filled with 1's and 0's. They then need to be of the ASCII system designation. The '0' and '1' are character values not integer values such as in your array declaration: s[limit]. The "limit" is a single int value of; 100000. Your array[] is named "s" that is of a data-type "char" that stores characters; you've assigned a single numerical value. (please correct me if I am thinking incorrectly, or, can better my own coding) TY>

include

include

using namespace std; int const limit = 100000;

int main() {

int k;
char s[limit];



cout << "Enter a k limit: \n" ;
cin >> k;
cout << "You entered: " << k << endl;
cout << "Enter your 0's and 1's. \n";
cin >> s;
cout << "you entered: " << s << '\n';



for (int j = 0; j < k; j++) {
    for (int i = 0; i < sizeof(s); i++) {
        if (s[i + 1] == '0' && s[i] == '1') {
            s[i + 1] = '1';
            s[i] = '0';
        }
    }
    cout << s;

}
return 0;

}

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