Extract a Byte C++ - c++
I have
int chop (char* input, unsigned int length)
{
for (int chopper = 0; chopper < = length; chopper++)
{
//how to pass 1byte this input to another function
//Will this for loop help?
}
}
How do I extract one byte of from this input for my further processing?
Thank you
int chop (char* input, unsigned int length)
{
for (int chopper = 0; chopper < = length; chopper++)
{
doSomething(input[chopper]);
}
}
What's wrong with
for (int chopper = 0; chopper < length; chopper++)
{
//how to pass 1byte this input to another function
//Will this for loop help?
unsigned char byte = input[chopper];
/// do whatever with the byte, and then move on to the next one
}
?
Note, that chopper < = length is probably wrong, you most likely want chopper < length.
You can treat the pointer as a read-only array, so you'd refer to a single char of your input like this:
input[chopper]
You should also probably change the loop's end condition to
chopper < length
as otherwise your loop's last iteration will refer to a memory location beyond input's size (you start from 0).
Related
Find Number of Palindrome Strings in Array Program Errors
I am writing code that finds the number of palindrome strings in a given array of strings. I think I have the right idea, but I am getting weird errors when I run it. What exactly am I doing wrong? int countPalindromes(string s) { int size = s.size(); int counter = 0; string forwardSum = ""; string backwardSum = ""; for(int i = 0; i < size; i++){ for(int j = i; j < size; i++){ forwardSum.push_back(s[j]); backwardSum.push_back(s[(n - 1)-j]); if(forwardSum == backwardSum){ counter++; } } } return counter; }
string forwardSum[] = {}; This is an array of zero size (which I don't believe is legal but we'll let that pass) forwardSum[i] = forwardSum[i] + s[j]; This is an attempt to access the ith element of an array which has zero size. That's bad. I'm not really following your code (it's late at night), but I think you probably want forwardSum and backwardSum to be strings not arrays of strings. And you probably want to use push_back to add characters from s to those strings. I.e. string forwardSum; ... forwardSum.push_back(s[j]); // add s[j] to forwardSum But if you really do want forwardSum to be an array, then the sensible thing to do would be to use a vector instead. vector<string> forwardSum(size); // a vector of strings with the given size Now that should at least not crash with the rest of your code.
Generate random char/digit
I`m trying to found fastest way to generate random digit/char array. char *randomGet(int num) { srand(time(NULL)); const char ab[37] = { "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ" };//Alphabet&Digit char *targ = new char[num]; for (int i = 0; i < num; i++) { strcat(targ, ab[rand() % 38]); } return targ; } So far I've come up with this, but it does not work (argument of type char is incompatible with parameter of type const char *). Help me find the best solution to my problem. Ty.
strcat() takes a char* as input, but you are giving it a single char instead, thus the compiler error. Also, the buffer that strcat() writes to must be null terminated, but your targ buffer is not null terminated initially, and you are not allocating enough space for a final null terminator anyway. You don't need to use strcat() at all. Since you are looping anyway, just use the loop counter as the index where to write in the buffer: Also, you are using the wrong integer value when modulo the return value of rand(). You are producing a random index that may go out of bounds of your ab[] array. Try this instead: char *randomGet(int num) { srand(time(NULL)); static const char ab[] = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ"; //Alphabet&Digit char *targ = new char[num+1]; for (int i = 0; i < num; ++i) { targ[i] = ab[rand() % 36]; } targ[num] = '\0'; return targ; }
I'd make two changes. First, make the internal source array static: static const char ab[] = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ"; Note that this version does not specify the array size; the compiler will figure it out from the initializer. Second, pass in a pointer to the target array: void randomGet(char* targ, int num) { static const char ab[] = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ"; for (int i = 0; i < num - 1; ++i) targ[i] = ab[rand() % (sizeof ab - 1)]; targ[num - 1] = '\0'; } This way, the caller decides how to allocate memory for the string.
Filling and comparing char* inside a function
I wrote the function readChar() which is designed to read the characters send by my WiFi module one by one(function works has advertised) and pass them to a char buffer of increasing size. The function should stop when char *endChar (multiple characters) have been detected or the number of character returned by timedRead() has exceeded size_t length. I have several issues: 1/. I don't understand the syntax (found inside the Arduino Stream library) : *buffer++ = (char)c; Can you explain how the array buffer gets filled? And why buffer[index] = (char)c; doesn't work here? 2/. I would like to compare buffer and endChar in the loop, possibly by using strcmp(buffer,endChar) (maybe there is a better way). But that doesn't seem to work. In fact when printing the ASCII values of my char *buffer then seem to increment from the end of the buffer. E.G.: So what is the best way to do that comparison? The code, inserted in the loop: _dbgSerial->println("buffer"); for (int i = 0; i < 32; i++){ _dbgSerial->print(buffer[i], DEC); _dbgSerial->print(","); } _dbgSerial->println(""); prints: buffer 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13,10, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13,10,13, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13,10,13,10, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13,10,13,10,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13,10,13,10,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13,10,13,10,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,13,10,13,10,0,0,0,0, Here is the function readChar(): size_t Debugwifi::readChar(char *endChar, char *buffer, size_t length) { if (length < 1) return 0; size_t index = 0; while (index < length) { int c = timedRead(); if (c < 0 ) break; //buffer[index] = (char)c; *buffer++ = (char)c; _dbgSerial->println("buffer"); for (int i = 0; i < 32; i++){ _dbgSerial->print(buffer[i], DEC); _dbgSerial->print(","); } _dbgSerial->println(""); if (strcmp(buffer,endChar)==0) { break; _dbgSerial->println("brk");} index++; } return index; }
As Rickard has explained, *buffer++ = (char)c; is how you assign a character to the memory a pointer points at, and then increment the pointer. However, your function has a lot of problems - you keep comparing unset memory with *endChar. I suggest: size_t Debugwifi::readChar(const char * const endStr, // const pointer to const. char * const buffer, const size_t length) { if (length < 1) return 0; const size_t endLen = strlen(endStr); for (size_t index = 0; index < length; index++) { const int c = timedRead(); if (c < 0 ) break; buffer[index] = (char)c; // Debug _dbgSerial->println("buffer"); for (size_t i = 0; i < length; i++){ // Better to use size_t here, // and compare against length not 32 _dbgSerial->print(buffer[i], DEC); _dbgSerial->print(","); } _dbgSerial->println(""); // Finished? if (index >= endLen) { if (memcmp(&buffer[index-endLen], endStr, endLen)==0) { _dbgSerial->println("brk"); // Must do this *before* "break"! break; } } } return index; } I have added a lot of consts. It's hard to have too many. The important point is that once you have read enough characters, to start comparing the last characters you have read to the end marker. Note that this function does not remove the end marker, and if you pass a 32-byte zero-filled array and it reads 32 characters, the result will NOT be zero terminated. Finally, I changed the argument name to endStr because I had expected endChar to be a pointer to a single character - not a NUL-terminated string.
*buffer++ = (char) c; First writes the value of c to what buffer is currently pointing to. Then it increments the buffer This is also why your loop to print buffer doesn't work. You start printing from the position after what was just filled. This is also why your strcmp doesn't work. It doesn't actually compare what you have filled your buffer with. It compares the content beyond what have been filled. If you want your printing code to work you should save the initial value of buffer before the loop; const char *buffer_start = buffer; Then use that in your printing code instead of buffer.
Inputting numbers in char array
In reference to this problem I need to input a 100 digit number in the program and perform operations on it. I figured I would use a char array to do it. I wrote this code after trying for some time. int main() { //int cases=10; //while(cases--) //{ int i; char totalapples[102]; //char klaudia_apples[200] , natalia_apples[200]; for(i=0;totalapples[i]!='\0';i++) { cin>>totalapples[i]; } //cin>>moreapples[200]; //cout<<moreapples[200]; while(i--) { cout<<totalapples[i]; } //} return 0; } When I run this, I get the following result: Input : 1234657891234567981345698 Output : 987564321 Can anybody tell what is happening??
Your program invokes undefined behavior. Despite the fact the char array contains undefined values you attempt to read from it using totalapples[i]!='\0' in your for loop. Instead, read the whole string into a std::string: string totalApplesStr; cin >> totalApplesStr; Now you can iterate over totalApplesStr: for (char c : totalApplesStr) { /* Do whatever you need to */ } Or, if that isn't a logical error in your code, you can iterate from the end to the beginning: for (auto it = totalApplesStr.rbegin(); it != totalApplesStr.rend(); ++it) { /* Do whatever you need to */ }
Use std::string instead of a char array. You may then use std::tranform to convert to a vector of int to do your big number computations.
You attempt to test values from an array that has not been initialized yet. Although a char array is the least safe method you could use for this, you could fix it with an initializer: char totalapples[102] = {0}; Also, your second loop will print the result backwards. Try incrementing to i, not decrementing from i.
You new initialize your counter/index. And there are many other improvement to be done, but your code was short #define ARRAY_SIZE 102 int main { char totalapples[ARRAY_SIZE]; unsigned int i = ARRAY_SIZE; i = 0; while (i < ARRAY_SIZE) { cin>>totalapples[i]; // range is 0..101 i++; } i = ARRAY_SIZE; while (i > 0) { cout<<totalapples[i]; // range is 0..101 i--; } }
C++ creating a two-dimensional array based on user input string length
I have an assignment to create a block transposition cipher program. A user is to input a phrase of their choice, and the program is to strip the phrase of spaces, punctuation, and make lowercase, before reading its length and creating a two-dimensional array the size of the nearest square that will fit all the chars in the mutated string, and filling in the remaining space with random letters. Problem is, I'm having issues with creating that square. I have this so far: int main() { string input; cout << "Please enter message to cipher." << endl; getline(cin, input); /* do punctuation removal/mutation */ int strLength = input.length(); //after mutation /* need to find the square here before applying sizes and values to arrays */ char * original = new char[][]; // sizes pending char * transposed = new char[][]; // sizes pending for (int i = 0; i <= /* size pending */ ; i++) { for (int j = 0; j <= /* size pending */ ; j++) { transposed[j][i] = original[i][j]; } } /* do more stuff here */ } any ideas? (I already have done the mutation portion; tested with alternate code)
You can't do e.g. char * original = new char[][]; First of all you are trying to create an array of arrays (or pointer of pointers) and assign it to a single pointer. You need to do it in two steps: Allocate the "outer" array: char **original = new char* [size]; Allocate the "inner" strings: for (int i = 0; i < size; i++) original[i] = new char [other_size]; However I would highly recommend against using that! Instead you should be using std::vector instead of "arrays" allocated on the heap, and if the contents are strings then use std::string: std::vector< std::vector< std::string > > original;
You can take the square root of the length, round down to an integer, and add one to get the new length. int len = (int)(sqrt(strLength) + 1e-9) + 1; You'd then malloc the square using len and fill as you normally would.
I believe you do not need the "new" to create your storage. Following code should just do the job: char buf[size][size]; // size is a variable ... // populate your buf char tmp; for(int i = 0; i < size; i++) { for(int j = 0; j < i; j++) { tmp = buf[i][j]; buf[i][j] = buf[j][i]; buf[j][i] = tmp; } } This does the transpose in place. You don't need another array to store the char's.