QT QBytearray count character - qt

How I can count characters in QByteArray, for example I have QByteArray and I want to know how many "*" in this array.

From QByteArray documentation:
int QByteArray::count ( const char * str ) const
This is an overloaded function.
Returns the number of (potentially overlapping) occurrences of string str in the byte array.
count.

You could use QByteArray::indexOf(char ch, int from = 0) const inside a loop.
Maybe this:
int i = 0, counter = 0;
while((i = array.indexOf("*", i)) >= 0)
counter++;

Related

Invalid operands to binary expressions

I was solving N queens problem and I wrote isvalid function, but the function gives "invalid operands to binary expressions" error. The error occurs at board[X][Y] == 'Q':
bool isvalid(vector<vector<string>>& board , int &row , int &col , int &n){
int x = row ;
int y = col ;
while(y >= 0){
if(board[x][y] == 'Q'){// THE ERROR IS OCCURING HERE AT BOARD[X][Y]== 'Q'
return false;
}
y-- ;
}
int x = row ;
int y = col ;
while(x >=0 and y>=0){
if(board[x][y] == 'Q')return false;
x-- ;
y-- ;
}
int x = row ;
int y = col ;
while(x < n and y>=0){
if(board[x][y] == 'Q')return false;
x++ ;
y-- ;
}
return true
}
The problem is that there's no corresponding operator== that could be called for the comparison. board[x][y] is a std::string, whereas 'Q' is a char. As you can see on cppreference, second parameter of operator== for comparison with std::string (which is std::basic_string<char>) can either be another std::string (taken by const reference) or const char* (C-style string). Neither of these is char or could be obtained through implicit conversion from char. In particular, see that there's no constructor of std::string taking a single character.
So, the easiest solution is to compare with a string literal "Q" instead. This is an array object of type const char[2] (second character is a null terminator '\0'), which decays into const char* pointer through array-to-pointer conversion, so appropriate overload of operator can be used:
board[x][y] == "Q"
Also, note that you missed a ; after the return statement.

Arduino - How to convert double to HEX format

I have an arudino code where I get some temperature reading:
double c1 = device.readCelsius();
Serial.println(c1);
The output is for example: 26.23
What I need is to get this converted to 2623 and then to HEX value so I get: 0x0A3F
Any clue?
I guess your float values always get numbers up to two decimal. So, you can just multiply the value which you read from sensor with a 100.
decimalValue = 100 * c1
And then you can use this small code for converting the decimal value to HEX.
Thanks to GeeksforGeeks
You can find the full tutorial here
// C++ program to convert a decimal
// number to hexadecimal number
#include <iostream>
using namespace std;
// function to convert decimal to hexadecimal
void decToHexa(int n)
{
// char array to store hexadecimal number
char hexaDeciNum[100];
// counter for hexadecimal number array
int i = 0;
while (n != 0) {
// temporary variable to store remainder
int temp = 0;
// storing remainder in temp variable.
temp = n % 16;
// check if temp < 10
if (temp < 10) {
hexaDeciNum[i] = temp + 48;
i++;
}
else {
hexaDeciNum[i] = temp + 55;
i++;
}
n = n / 16;
}
// printing hexadecimal number array in reverse order
for (int j = i - 1; j >= 0; j--)
cout << hexaDeciNum[j];
}
// Driver program to test above function
int main()
{
int n = 2545;
decToHexa(n);
return 0;
}

Hash to string with given character set

The usual hash-functions, e.g. from digest create hex output. I want to create a hash with character from a given set, e.g [a-z,0-9]; no strong cryptographic security is required.
Using base64encode on a hashed string comes close, but the character set is fixed in that function.
It is ugly div/mod manipulation for an arbitrary character table, so I decided to use a 32 character table without l 0, O
#include <Rcpp.h>
using namespace Rcpp;
static const std::string base32_chars = "abcdefghijkmnpqrstuvwxyz23456789";
// [[Rcpp::export]]
String encode32(uint32_t hash_int, int length = 7)
{
String res;
std::ostringstream oss;
if (length > 7 || length < 1)
length = 7;
for (int i = 0; i < length; i++) {
oss << base32_chars[hash_int & 31];
hash_int = hash_int >> 5;
}
res = oss.str();
return res;
}
/*** R
print(encode32(digest::digest2int("Hellod")))
*/

how to split a string into words in arduino?

I have a string in arduino
String name="apple orange banana";
Is it possible to store each item in an array arr
so that
arr[0]="apple"
arr[1]="orange" ......etc
if not store them in individual variables?
How to split a string using a specific delimiter in Arduino? I believe this would help you, you could do a while loop like:
int x;
String words[3];
while(getValue(name, ' ', x) != NULL){
words[x] = getValue(name, ' ', x);
}
Using this function:
// https://stackoverflow.com/questions/9072320/split-string-into-string-array
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
If you know your list length and the max characters per list item, you could do
char arr[3][6] = {"apple", "orange", banana"};
edit: if you are looking for something like String arr[3] you aren't going to get it because of how memory is managed with the C language

Finding all possible permutations of the characters in a set of strings using recursion

I have this set of (Greek) strings:
ἸἼΙἹἽ,
ῇηἤήῃὴῆἡἠἢᾖἥἣῄἦᾗᾐἧᾔᾑ,
σς,
οὸόὀὄὅὂ,
ὺὖυῦύὐὑὔΰϋὕὗὓὒῢ
I'd like to find all possible permutations of the characters in these 5 strings. For example, Ἰῇσοὺ, Ἰῇσοὖ, Ἰῇσου, etc. I know it should involve recursion since the number of strings is not fixed but I'm a beginner and I'm completely dumbfounded by recursion.
I did the following in Python and it does give me all combinations of the characters in each string. But I need the 'ἸἼΙἹἽ' to always come first, 'ῇηἤήῃὴῆἡἠἢᾖἥἣῄἦᾗᾐἧᾔᾑ' second,'σς' third, etc.
# -*- coding: utf-8 -*-
def Gen( wd, pos, chars ):
if pos < len( chars ):
for c in chars:
for l in c:
Gen( wd + l, pos + 1, chars )
else:
print wd
chars = [ u'ἸἼΙἹἽ', u'ῇηἤήῃὴῆἡἠἢᾖἥἣῄἦᾗᾐἧᾔᾑ', u'σς', u'οὸόὀὄὅὂ', u'ὺὖυῦύὐὑὔΰϋὕὗὓὒῢ' ]
Gen( "", 0, chars )
Thanks for the help everybody. My mind is completely blown. Recursion! Here's what I ended up doing in Python:
# -*- coding: utf-8 -*-
s = [ u'ἸἼΙἹἽ', u'ῇηἤήῃὴῆἡἠἢᾖἥἣῄἦᾗᾐἧᾔᾑ', u'σς', u'οὸόὀὄὅὂ', u'ὺὖυῦύὐὑὔΰϋὕὗὓὒῢ' ]
results = []
def recur( wd, strings ):
index = 0
if index < len( strings ):
for c in strings[ index ]:
recur( wd + c, strings[ index + 1: ] )
else:
results.append( wd )
def main():
recur( '', s )
for r in results:
print r.encode( 'utf-8' )
main()
You create a char array which will contain the string you want to work with
char str[] = "ABC";
then you get the length of the string int n = strlen(str); and lastly you permutate.
You make a new function which will contain the input string, starting index of the string and ending index of the string.
Check if the starting index (int s) equals the ending index (int e)
if it does, that means you're done, if not you go into a loop where you go from start (s) to end (e), swap the values, recurse, swap again to backtrack.
An example in C++:
#include <stdio.h>
#include <string.h>
void swap(char *i, char *j)
{
char temp;
temp = *i;
*i = *j;
*j = temp;
}
void permutate(char *str, int start, int end)
{
int i;
if (start == end)
printf("%s\n", str);
else
{
for (i = start; i <= end; i++)
{
swap((str + start), (str + i));
permutate(str, start + 1, end);
swap((str + start), (str + i)); //backtrack
}
}
}
int main()
{
char str[] = "ABC";
int n = strlen(str);
permutate(str, 0, n - 1);
return 0;
}
I'm not that familliar with Python, but I've found something that might help in your case:
def comb(first_str, second_str):
if not first_str:
yield second_str
return
if not second_str:
yield first_str
return
for result in comb(first_str[1:], second_str):
yield first_str[0] + result
for result in comb(first_str, second_str[1:]):
yield second_str[0] + result
>>> for result in comb("ἸἼΙἹἽ", "ῇηἤήῃὴῆἡἠἢᾖἥἣῄἦᾗᾐἧᾔᾑ"):
print(result)
Just write down the five nested loops. In pseudocode,
for a in "ἸἼΙἹἽ"
for b in "ῇηἤήῃὴῆἡἠἢᾖἥἣῄἦᾗᾐἧᾔᾑ"
for c in "σς"
for d in "οὸόὀὄὅὂ"
for e in "ὺὖυῦύὐὑὔΰϋὕὗὓὒῢ"
emit [a,b,c,d,e]
To encode these five loops with recursion, so it's good for any number of input strings, again in pseudocode,
g(list-of-strings) =
| case list-of-strings
| of empty --> end-of-processing
| of (first-string AND rest-of-strings) -->
for each ch in first-string
DO g(rest-of-strings)
Now you only need to figure out where to hold each current first-string's character ch and how to combine them all while at the end-of-processing (basically, your two options are a global accumulator, or an argument to a function invocation).

Resources