Home > Article > Backend Development > Check the score of a given binary string
A sequence of bytes is called a binary string, which holds a binary value. Binary scores are usually expressed in the range 0 to 1, with 1 reserved for perfect models. In the given binary string, if an element is found to be 1, then it is calculated as a fraction and the count sum is increased.
Let us illustrate with an example of a binary fraction -
The given binary string is 1011010.
In the above image, the number 1 appears at index 0, 2, 3 and 5.
Therefore, the total score is 4 because there are 4 indicators with a binary score of 1.
The given binary string is 0110001.
In the above image, the number 1 appears in index 1, 2 and 6.
Thus, the total score is 3 because there are 3 indexes with a binary score of 1.
In this article, we will find the score of a given binary string.
The following syntax used in the program
string_name.length()
string_name
length() − This function calculates the length of a string in bytes
We will start the program with a header file called 'iostream'.
Starting from the main function, we will declare a string variable "binary_str" and initialize it with a binary string. At the same time, initialize the count variable and set its value to 0.
We will create a for loop variable and set it to a counter based on the length of the binary string.
In the for loop, we will use the if statement to check whether the i-th character of binary_str is equal to 1. If the i-th character of the binary string is equal to 1, the count will be incremented. Once the for loop completes we will get the final count which will be the fraction of the given binary string.
After the loop completes, we simply print the message as "The binary fraction of the given number is:" followed by the count value.
In this program, we will use a for loop to implement the fraction of a binary string. (Tip - if the index position of the string is 1, count it as a fraction)
#include <iostream> using namespace std; int main() { string binary_str = "101110101001"; int count = 0; for( int i = 0; i <= binary_str.length(); i++ ) { if( binary_str[i] == '1' ) { count++; } } cout<<"The binary score of given number is:\t"<<count; return 0; }
The binary score of given number is: 7The Chinese translation of
In this program, we will use a while loop to implement scoring of binary strings. (Tip - If the index position in the string is 1, it will be counted as a score)
#include<iostream> using namespace std; int main() { string binary_str = "1001"; int count = 0; int i = 0; while( i <= binary_str.length() ) { if(binary_str[i] == '1') { count++; } i++; } cout<<"The binary score of given number is:\t"<<count; return 0; }
The binary score of given number is: 2
We explored the concept of binary string scoring and saw how length is used to calculate the score of a given binary string. Generally speaking, computers understand only two numbers - 0 and 1, and use them to perform various functions. For example - For any IoT device, both 0 and 1 are important.
The above is the detailed content of Check the score of a given binary string. For more information, please follow other related articles on the PHP Chinese website!