Thursday, December 3, 2020

, , , ,

strtok Fucntion in c with example Program for strtok

  •  strtok function in c programming will help you to give the token of given input.
  • This kind of function (strtok in c ) is very useful which reduce the length of c program.
strtok in c
 strtok

 

Use of the strtok function in c

strtok is an inbuilt function which is part of the <string.h> c header file.

Let's see first : - 

#include <string.h>
 
char* strtok(char* str, const char* delim);
  • This function take an input str and also a delimiter character delim.
  • Then strtok() in c split the input string into the tokens which is based on the delimited character.

Return Value of strtok in c

  • This function return a single string.
  • strtok() in c call this function continuously until we get NULL input sting.

Example for strtok in c

#include <stdio.h>
#include <string.h>
 
int main() {
    // Our input string
    char input_string[] = "Hello from function in c!";
 
    // Our output token list
    char token_list[20][20]; 
 
    // We call strtok(input, delim) to get our first token
    // Notice the double quotes on delim! It is still a char* single character string!
    char* token = strtok(input_string, " ");
 
    int num_tokens = 0; // Index to token list. We will append to the list
 
    while (token != NULL) {
        // Keep getting tokens until we receive NULL from strtok()
        strcpy(token_list[num_tokens], token); // Copy to token list
        num_tokens++;
        token = strtok(NULL, " "); // Get the next token. Notice that input=NULL now!
    }
 
    // Print the list of tokens
    printf("Token List:\n");
    for (int i=0; i < num_tokens; i++) {
        printf("%s\n", token_list[i]);
    }
 
    return 0;
}


We use the strtok(NULL, " ") function in c and use the loop for until the get NULL.

Output strtok in c

Token List:
Hello
from
Function
in
c

Tuesday, December 1, 2020

,

Example program for map() function in python

 map() function in Python applies a given function to every item of an iterable and give a list of results.

Syntax:-

map(function, iterable, ...)

Parameter For map() function in python 

map() function use two Parameter these are follow : - 

  •  function is a first Parameter map() function passes every item of iterable to the given function.
  • iterables are second which is going to to be mapped.

 Return value : - map() function in python return the list of result.

Example of map() function in python

def calculateSquare(n):
    return n*n


numbers = (1, 2, 3, 4)
result = map(calculateSquare, numbers)
print(result)

# converting map object to set
numbersSquare = set(result)
print(numbersSquare)

OUTPUT : -

map() function output
 map() function output

Well Python lambda Function commonly used with map () python function.



Monday, November 30, 2020

, , ,

date difference function in sql with example program

  •  datediff sql function will helps to calculate the difference between two dates in week,year,months etc.
  • datediff sql function accept the three arguments start_date, end_date and date_part.
  •  date_part is a part of the date like year months and week that's compare between the start_date and end_date.
  • start_date and end_date are two dates which is going to be compare.They contain the value in the form of  type DATE, DATETIME, DATETIMEOFFSET, DATETIME2, SMALLATETIME, or TIME.

Table for date_part for datediff sql function

sql server datediff
sql server datediff

Return value of sql server datediff function

SQL DATEDIFF() function will return the integer value to indicate the  difference between the start_date and end_date and specified by the date_part.

sql server datediff function return the error if range of the integer return value is (-2,147,483,648 to +2,147,483,647).

 Example for  datediff SQL function  for SQL Server

  •  let see the differences between two date value 

Now use the DATEDIFF() function in SQL to compare two dates dates in various date parts:

DECLARE 
    @start_dt DATETIME2= '2019-12-31 23:59:59.9999999', 
    @end_dt DATETIME2= '2020-01-01 00:00:00.0000000';

SELECT 
    DATEDIFF(year, @start_dt, @end_dt) diff_in_year, 
    DATEDIFF(quarter, @start_dt, @end_dt) diff_in_quarter, 
    DATEDIFF(month, @start_dt, @end_dt) diff_in_month, 
    DATEDIFF(dayofyear, @start_dt, @end_dt) diff_in_dayofyear, 
    DATEDIFF(day, @start_dt, @end_dt) diff_in_day, 
    DATEDIFF(week, @start_dt, @end_dt) diff_in_week, 
    DATEDIFF(hour, @start_dt, @end_dt) diff_in_hour, 
    DATEDIFF(minute, @start_dt, @end_dt) diff_in_minute, 
    DATEDIFF(second, @start_dt, @end_dt) diff_in_second, 
    DATEDIFF(millisecond, @start_dt, @end_dt) diff_in_millisecond;

OUTPUT

DATEDIFF() function in SQL
DATEDIFF() function in SQL

Sunday, November 29, 2020

, ,

meshgrid MATLAB Function With It's example surface plot of a function.

meshgrid matlab Function:-

Helps to generate X and Y matrices for three-dimensional plots.

Syntax For meshgrid: -

[X,Y] = meshgrid(x,y)
[X,Y] = meshgrid(x)
[X,Y,Z] = meshgrid(x,y,z)

Description for meshgrid matlab Function

  • meshgrid function will help to trans transforms the given domain which is specified by the  x and y into arrays X and Y and these array can be used to check the two variables and three-dimensional mesh/surface plots.
  •  Output rows of array X will be copies of x vector; same as the columns  output array Y will be copies of y vector
  •  [X,Y] = meshgrid(x) is like [X,Y] = meshgrid(x,x).
  •  [X,Y,Z] = meshgrid(x,y,z) helps to produces the three-dimensional arrays which is used to check the desired functions of three variables and three-dimensional volumetric plots.

 Important Note for meshgrid matlab Function

  •  The meshgrid function in MATLAB is same as the ndgrid function but the point that should be remembered that is "in meshgrid function the order of  first two input and output arguments is switched"

     [X,Y,Z] = meshgrid(x,y,z)

gives the same result as

    [Y,X,Z] = ndgrid(y,x,z)

  •   meshgrid function in MATLAB is surely suited for two- or three-dimensional Cartesian space.
  •  ndgrid fucntion in MATLAB is suited for multidimensional problems that are not spatially based.

 Example for meshgrid matlab Function

    [X,Y] = meshgrid(1:3,10:14)

    X =

         1     2     3
         1     2     3
         1     2     3
         1     2     3
         1     2     3

    Y =

        10    10    10
        11    11    11
        12    12    12
        13    13    13
        14    14    14

use meshgrid to create a surface plot of a function.

    [X,Y] = meshgrid(-2:.2:2, -2:.2:2);                                
    Z = X .* exp(-X.^2 - Y.^2);                                        
    surf(X,Y,Z)


meshgrid to create a surface plot of a function.
meshgrid to create a surface plot of a function.

Friday, November 27, 2020

,

ord()Function in python with it's example

 ord function in python is used to return an integer representing of Unicode character.

Syntax:-

ord(ch)

Parameters For ord function in python

This function take just only one perameter and that known as ch (a Unicode character)

Return value

The ord() function in python returns an integer representing the Unicode character.

Example for ord()Function in python

print(ord('5'))    # 53
print(ord('A'))    # 65
print(ord('$'))    # 36

OUTPUT

ord()Function
ord()Function in python

Wednesday, November 25, 2020

, ,

hasattr() function in python with example

 Python hasattr() function in python return the true if the object gives the name of attribute and false if didn't return the name.

Syntax for hasattr()

hasattr(object, name)

hasattr() in python called by the getattr() to check about the error


Parameters For hasattr() function in python

There are two parameters which is used for this function the first one is object whose name is going to be checked.

And the name is second parameter which give the name for searched

Return value

It gives boolean return value True and False

Program Example For hasattr() in Python

class Person:
    age = 22
    name = 'Adam stiffman'

person = Person()

print('Person has age?:', hasattr(person, 'age'))
print('Person has salary?:', hasattr(person, 'salary'))

Output

Person has age?: True
Person has salary?: False

Tuesday, November 24, 2020

, , ,

Use of Range Function In Python With Program

 python range function in python return the sequence of numbers between the start integer value to the stop integer.

Syntax : -

range(stop)
range(start, stop[, step])

Parameters For range() function in python:-

Range function takes the three arguments which is given below :-

  •  first one is a start point which define the starting point of range function.
  • second one is stop point integer which help to tell the end point for the sequence.
  • Step is a third one and it use to define the increment for the sequence of number.

Return value from range function in python: -

  •  range() function return the immutable sequence of number.
  • In this function sequence of number starts form 0 to Stop-1.
  • if the step argument is then it raise an Value Error.

How to use the range function in python with program

# empty range
print(list(range(0)))

# using range(stop)
print(list(range(10)))

# using range(start, stop)
print(list(range(1, 10)))

Output:

range function program in python
range function program in python