Showing posts with label Built in Function. Show all posts
Showing posts with label Built in Function. Show all posts

Thursday, March 18, 2010

Built-in-Functions (String) – Enjoy it!

Let us plunge to another episode of functions, I know it is boring; at the same time it is most handy when you want to finish a program with lots of mathematical formula. Last week I have gone through a few numerical functions and the way functions help us for converting one data type to other. Today mainly I will discuss about string function, which only valid to string type data. String handling is not an easy job and you will find many functions available for this, but often while you write a program, you will be asked to write your own functions only and prohibited from using some of the string function. So, how to do this I will come to this when we talk about programming. String functions in Java return value is string but some return integer also. In java java.lang is default package so you do not need any extra package to be import but in C/C++ you require "string.h".

Note : Str - is the String argument you have to pass and Num is a Number of character and pos is the position.

DescriptionBasicC/C++Java
Find the length of a string value
len(Str$)

strlen(Str)

Str.length()
Extract a character from the said string from given
position

mid$(Str$,pos,num)

str(pos)

Str.charAt(pos)
Check equality between two string and return a
Boolean value/

Str1.equals(Stri2)
Check equality between two string ignoring he case
difference and return a Boolean value.

Str1.equalsIgnorecase(Str2)
Compare between two string( relevant to the ASCII
values of each character), if both equal it return 0, if String1 is
smaller than String2 then it will return -1 and if String1 is bigger
than String2 then it will return 1.

strcmp(Str1,Str2)
strcmpi(Str1,Str2)
(strcmpi() - for ignoring
case)

Str1.compareTo(Str2)
Extract all the string from given position till end.

String.substring(int)
Extract all the string from starting position up to
left of end position.

mid$(Str$,pos,num)
left$(Str$,num)
right$(Str$,num)
Using any loop from 0 position till
length of the string, we can extract character by character
Str1.substring(int,int) -
Join String2 to the right of String1Str1$+Str2$strcat(Str1,Str2)
Str1.concat(Str1)
This can be done by:
String1=String1+ String2.
Returns the index within this string of the first
occurrence of the specified character.

Str.indexOf(char/string)
Returns the index within this string of the first
occurrence of the specified character, starting the search at the
specified index.

Str.indexOf(char/string,
int)
Returns the index within this string of the last
occurrence of the specified character.

Str.lastIndexof(char/String)
Tests if this string starts with the specified
prefix.

Str.startswith(String)
Tests if this string ends with the specified suffix.
Str.endsWith(String)
Removes white space from both ends of this string.
Str.trim()
Returns a new string resulting from replacing all
occurrences of oldChar in this string with newChar.

Str.replace(char, char)
Str.replace(Str1, Str2)
Converts this String to lowercase / Converts this
String to upper case.
strlwr(Str) / strupr(Str)Str.toLowerCase() / Str.toUpperCase()
Converts this string to a new character array.Str.toCharArray()
In java we also having StringBuffer, so giving you some idea of this class-

A string buffer implements a changeable sequence of characters. String buffers are safe for use by multiple threads. The methods are
synchronized where necessary so that all the operations on any particular
instance behave as if they occur in some serial order. String buffers are used by the compiler to
implement the binary string concatenation operator +. For example, the code:

x = "a" + "4" +"c";

is compiled to the equivalent of:

x = new StringBuffer().append("a").append(4).append("c").toString()

Functions under StringBuffer
DescriptionJava
Constructs a string buffer with no
characters in it and an initial capacity specified by the length
argument.

StringBuffer()

Constructs a string buffer with no
characters in it and an initial capacity of 16 characters.

StringBuffer(int)
Constructs a string buffer so that it
represents the same sequence of characters as the string argument.

StringBuffer(String)
Appends the string representation of the
boolean argument to the string buffer.

append(boolean)
Appends the string representation of the
char argument to this string buffer.

append(char)
Appends the string representation of the
char array argument to this string buffer.

append(char[])
Appends the string representation of a
subarray of the char array argument to this string buffer.
append(char[], int, int)
Appends the string representation of the
int, float or double argument to this string buffer.

append(int) /append(float) /append(double)
Returns the current capacity of the
String buffer.

capacity()
Returns the character at a specific
index in this string buffer.

charAt(int)
Inserts the string representation of the
char argument into this string buffer.

insert(int, char) **
Inserts the string representation of the
char array argument into this string buffer.

insert(int, char[])
Returns the length (character count) of
this string buffer.

length()
Characters are copied from this string
buffer into the destination character array.

getChars(int, int, char[], int)
Sets the length of this String buffer.
setLength(int)
The character at the specified index of
this string buffer is set to ch.
setCharAt(int, char)
The character sequence contained in this string
buffer is replaced by the reverse of the sequence.
reverse()
Converts to a string representing the data in this
string buffer.
toString()
** Insert can be used as append for numeric too.

Here is a example, how we can use string buffer
System.out.println("StringBuffer insert and append example!");

StringBuffer sb = new StringBuffer(0);

System.out.println(sb.insert(0, "vinod"));

int len = sb.length();

System.out.println(sb.insert(len, "Deepak"));

System.out.println(sb.insert(6, "Raj"));

System.out.println(sb.append("Mohit"));



Database packages also provide some date and database functions, while writing about database I certainly cover those functions. I also remind you that if you feel anything left out then can suggest me, your suggestion will be always welcome.
!!!I know most of the students are busy with their respective examination and hardly have time to join this function, so Aburz for all.!!!

Thursday, March 11, 2010

Built-in-Functions (Maths) – Enjoy it!

Readymade with zero price tag and unlimited access, that is how I can describe a built-in-function. All programming languages having certain amount of built-in-function, which written as subroutine or function to solve a certain requirement, while writing a program you can, call these functions by sending arguments that required or with no arguments.

It is not that we cannot solve these, but when it is available, we just use it. Built-in-functions come in different types depending upon data types – Numeric, Date, String, Database etc. I will cover this topic in two weeks and with a few which we use frequently.

Numeric or math function to solve a mathematic routine like square root and other. Many math functions in Java return value is double type but you can take help of  type casting to get it in integer. In java java.lang is default package so you do not need any extra package to be import but in C or C++, you have to include "math.h" but for min() and max() in C/C++ you require
"stdlib.h" or often "complex.h"
Note : Num - is the numeric argument you have to pass.

Description

Basic

C & C++

Java

Returns the square root of a double value.

sqr(num)

sqrt(num)

Math.sqrt(num)

Returns the absolute value of a double value.

abs(num)

abs(num)

Math.abs(num)

Returns of value of the first argument raised to the power of the second
argument.

pow(num,num)

Math.pow(num,num)

Returns the smaller of two Numeric values.

min(num,num)

Math.min(num,num)

Returns the larger of two Numeric values.

max(num,num)

Math.max(num,num)

Returns the trigonometric sine, cosine and tangent of an angle where
argument should be in radian.

sin(num)
cos(num)
tan(num)

sin(num)
cos(num)
tan(num)

Math.sin(num)
Math.cos(num)
Math.tan(num)

Returns the exponential number  (i.e., 2.718...) raised to the
power of a double value.

exp(num)

exp(num)

Math.exp(num)

Returns the smallest (closest to negative infinity) double value that is
not less than the argument and is equal to a mathematical integer. 
ceil(12.5) = 13,
ceil(-12.5) = -12

cint(num)

ceil(num)

Math.ceil(num)

Returns the largest (closest to positive infinity) double value that is
not greater than the argument and is equal to a mathematical integer.
floor(12.5) = 12,
floor(-12.5) = -13.

fix(num)

floor(num)

Math.floor(num)

Returns a random number between 0.0 and 1.0

rnd(num)

random()

Math.random()

Return a double number that represents the closest integer to the double
parameter. rint(2323.43) =23.0, rint(2323.83) =23.0

 
Conversion - while writing a program we often have to change a data type to another. In C/C++, you need to include "stdlib.h". Here some example

Description

Basic

C & C++

Java

Integer to String

str$(num)

itoa(num,string,10) *

Integer.toString(num)

float to String
 -
do -

fcvt(num,ndigit,&dec,&sign)

Float.toString(num)

double to String
 -
do -

Double.toString(num)

String to Integer

val(string)

Integer.parseInt(String)

String to Float
 -
do -

Float.parseInt(String)

String to Double
 -
do -

Double.parseInt(String)
* 10 is the radix or base value, indicate that String value should be converted in Decimal. Binary (2), Octal (8) and in Hexadecimal (16).

Now these days Fortran and Pascal can be seen anywhere in the scenario, but some engineering Colleges having Fortran in their 3rd semester, so few Mathematical function for these programming languages are given below :

Description

Fortran

Pascal

Returns the square root of a double value.

sqrt(num)

sqrt(num)

Returns the absolute value of a double value.

abs(num)

abs(num)

Returns of value of the first argument raised to the power of the second
argument.

sqr(num,num)

Returns the trigonometric sine, cosine and tangent of an angle where
argument should be in radian.

sin(num)
cos(num)
tan(num)

sin(num)
cos(num)
tan(num)

Returns the exponential number  (i.e., 2.718...) raised to the
power of a double value.

exp(num)

exp(num)

This returns a real number whose value is the integer part of the real
value x. For example, Int(8.3), give you 8

int(num)

Int(num)

Modulus eg. mod(8,5) will give you remainder 3.

mod(num,num)

Next week coming with String functions. One thing I like to tell you, I often write my own packages for certain things and call my home made built-in functions, as I always like the challenge, but give you a tip too -
!!!Do not take too many challenges while appearing for exam, as exam itself is too challenging, happy finally time to say you Allez ciao!!!