Explain this code in wording from declarations till the end of code #include <io
ID: 3744747 • Letter: E
Question
Explain this code in wording from declarations till the end of code
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char string[100];
char transaction[50][2];
int count_01 = 0, count_10 = 0;
cout << "enter the String::";
cin >> string;
int length = strlen(string);
for (int i = 0; i < length-1; i++) {
transaction[i][0] = string[i];
transaction[i][1] = string[i + 1];
if (string[i] == '0' && string[i + 1] == '1')
count_01++;
else if(string[i] == '1' && string[i + 1] == '0')
count_10++;
}
if (count_01 == count_10 && count_01!=0)
cout << " string accepted ";
else
cout << " string Rejected ";
return 0;
}
Explanation / Answer
Line 1: #include <iostream>
It is the predefined library function used for input and output also called as header files. iostream is the header file which contains all the functions of the program like cout, cin etc. and #include tells the preprocessor to include these header file in the program.
Line 2: #include <cstring>
This header file defines several functions to manipulate strings and arrays such as strlen() etc
Line 3: using namespace std;
A namespace is a form of scope in C++ that holds its own definitions for variables, functions, etc. For example, both cout and cin , along with some useful tokens like endl , are defined inside of std for use.
Line 4: int main()
main() is a pre-defined function whose Data type is int(integer). int is the return type of main function, which means the function will return integer type value at the end of execution of the function.
Line 5: char string[100];
This line declares a string of length 100 named string. Thus, the indices in the string will range from 0 to 99.
Line 6: char transaction[50][2];
It declares an array of strings where 50 is the number of strings transactions and 2 is the maximum length of each string.
Line 7: int count_01 = 0, count_10 = 0;
It initializes two integer values count_01 and count_10 with value zero.
Line 8: cout << "enter the String::";
It displays the line "enter the String" on the standard console output device.
Line 9: cin >> string;
It takes input from standard console input device and stores that in the character array string.
Line 10: int length = strlen(string);
This line saves the length of the string array in a integer variable called length.
Line 11: for (int i = 0; i < length-1; i++) {
This line initialize a for loop variable i=1. In each iteration, the value of i will be incremented by 1. The for loop will execute until the value of i is less than the string length.
Line 12: transaction[i][0] = string[i];
This line stores the ith character in the string to the first character of ith transaction.
Line 13: transaction[i][1] = string[i + 1];
This line stores the (i+1)th character in the string to the second character of ith transaction.
Line 14: if (string[i] == '0' && string[i + 1] == '1')
This line checks if string's consecutive character array are 01, then execute the code in line 16.
Line 15: count_01++;
Increment the value of a variable of count_01 i.e. count_01=count_01 +1
Line 16: else if(string[i] == '1' && string[i + 1] == '0')
This line checks if string's consecutive character array are 10, then execute the code in line 17.
Line 17: count_10++;
Increment the value of a variable of count_10 i.e. count_10=count_10 +1
Line 18: }
This curly brace ends the for loop.
Line 19: if (count_01 == count_10 && count_01!=0)
This line checks if value of count_01 and count_10 are equal and count_01 is not equals to zero, then execute the code in line 20.
Line 20: cout << " string accepted ";
It displays the message "string accepted " on the standard console output device.
Line 21: else
If the condition in Line 19 is not true, execute the following line.
Line 22: cout << " string Rejected ";
It displays the message "string Rejected " on the standard console output device.
Line 23: As the main() need to return an integer, return the value zero.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.