Write Your Own ATOI in C++

sudhir sharma
Updated on 17-Apr-2020 13:11:28

701 Views

atoi() function in c programming language is used to handle string to integer conversion. The function takes a string as an input and returns the value in integer type.Syntaxint atoi(const char string)Parameters Accepted − The atio() function accepted a string as an input which will be converted into integer equivalent.Return type − the function returns an integer value. The value will be the integer equivalent for a valid string otherwise 0 will be returned.Implementation of atoi() function −We take each character of the string and make the integer by adding the number to the previous result multiplied by 10.For negative ... Read More

Write Your Own memcpy and memmove in C++

sudhir sharma
Updated on 17-Apr-2020 13:07:34

993 Views

memcpy() function is an inbuilt function that is used to copy data from source location to destination location.Prototype of memcpy function −void * memcpy(void *destination_location, void *source_location, size_t size)We will character by character copy data from source to destination.Program to show the implementation of the solution,Example Live Demo#include #include void MemcpyFunc(void *dest, void *src, size_t n){    char *dataS = (char *)src;    char *dataD = (char *)dest;    for (int i=0; i

Writing OS Independent Code in C/C++

sudhir sharma
Updated on 17-Apr-2020 12:42:29

408 Views

A program that can interact with the operating system irrespective of the OS on which it runs.Most of the compilers of c/c++ have the power to define macros that detect OS.Some Macros of GCC compiler are −_WIN32: macros for 32 bit and 64-bit Windows OS._WIN64: macros for 64-bit Windows OS._UNIX: macros for UNIX OS._APPLE_: macros for macOS.Based on these macros defined, let’s create a program that will work irrespective of the OS −Example Live Demo#include using namespace std; int main() {    #ifdef _WIN32     system("dir");    #else     system("ls");    #endif     ... Read More

XOR of a Submatrix Queries in C++

sudhir sharma
Updated on 17-Apr-2020 12:41:02

163 Views

In this problem, we are given a N x N matrix and some queries, each query contains the top-left and bottom-right corner of the submatrix created from this matrix. Our task is to find the XOR of all elements of the submatrix defined by the querries.Let’s take an example to understand the problem, Inputarr[][] = {{1, 2, 3} {4, 5, 6} {7, 8, 9}} Querries: {0, 0, 1, 2} , {1, 2, 2, 2}Output1 15Explainationquerry 1 : 1^2^3^4^5^6 querry 2 : 6^9To solve this problem, we will find a prefix-XOR matrix to solve queries. The value of the matrix at ... Read More

Logical Operators in C++

sudhir sharma
Updated on 17-Apr-2020 12:38:22

182 Views

In c++ programming language, there are keywords that can be used in place of logical operators. The keywords are initially used in c when the keyboards didn’t support symbols like &&, !, ||, etc. Now, here are some written version of logical operators in c++.Operators and their written versions are −OperatorSymbolsWritten versionAnd operator&&andOr operator||orNot operator!notNot equal to operator!=not_eqBitwise and operator&bitandBitwise or operator|bitorBitwise XOR operator^And equal to operator&=and_eqOr equal to operator|=or_eqXOR equal to operator^=Program to show the implementation of our programExample Live Demo#include using namespace std; int main(){    int x=1, y=0;    coutRead More

XOR of Array Elements with Set Bits Equal to K in C++

sudhir sharma
Updated on 17-Apr-2020 12:36:26

284 Views

In this problem, we are given an array of n elements and an integer value k. Our task is to find XOR of all elements of the array that have set bits equal to k.Let’s take an example to understand the problem, Inputarray = {2, 12, 44, 103, 17} , K =3Output44To solve this problem, we will count set bit of every element of the array and compare it with k. If the number of set bits is equal to k, then we will push it to a vector and find XOR of all elements of the vector.For finding the ... Read More

Pass Parameters in PowerShell Function

Chirag Nagrekar
Updated on 17-Apr-2020 12:36:16

19K+ Views

You can pass the parameters in the PowerShell function and to catch those parameters, you need to use the arguments. Generally, when you use variables outside the function, you really don’t need to pass the argument because the variable is itself a Public and can be accessible inside the function. But in some cases we need to pass the parameters to the function and below is the example explains how you can write the code for it.The single parameter passed in function, function writeName($str){    Write-Output "Hi! there .. $str" } $name = Read-Host "Enter Name" writeName($name)Here, we are passing ... Read More

Variable Array Scope in PowerShell Function

Chirag Nagrekar
Updated on 17-Apr-2020 12:34:31

288 Views

Generally, when the variable is declared as a Public variable or outside the function in the script (not in any other variable or condition), you don’t need to pass that value to function because the function retains the value of the variable when the variable is initialized before the function is called.Examplefunction PrintOut{    Write-Output "your Name is : $name" } $name = Read-Host "Enter your Name" PrintOutIn the above example, $name variable is declared outside the function called PrintOut. So as the variable can be read inside the function, you can directly use the variable by its name.OutputEnter your ... Read More

Explain the PowerShell Function

Chirag Nagrekar
Updated on 17-Apr-2020 12:32:23

948 Views

Function in a PowerShell is to reduce the redundancy of the repeated codes this means codes which are repeating, bind them into a function and call the function whenever necessary, so you don’t need to write codes multiple times.ExampleLet assume, you want to perform arithmetic operations (multiply, sum, divide and subtraction) of two values 5 and 4 in one go, so you will write different operations for two values or you will assign values to the variable called $val1 and $val2 and perform various operations on them as shown below in the example.$val1 = 5 $val2 = 4 $val1 ... Read More

XOR Cipher in C++

sudhir sharma
Updated on 17-Apr-2020 12:28:55

3K+ Views

XOR cipher or XOR encryption is a data encryption method that cannot be cracked by brute-force method.Brute-force method is a method of random encryption key generation and matching them with the correct one.To implement this encryption method, we will define an encryption key(random character) and perform XOR of all characters of the string with the encryption key. This will encrypt all characters of the string.Program to show the implementation of encryption −Example Live Demo#include #include using namespace std; void XORChiper(char orignalString[]) {    char xorKey = 'T';    int len = strlen(orignalString);    for (int i = 0; i < len; ... Read More

Advertisements