Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

Thursday, 23 February 2012

String functions in C and C++ programming

,
String input\output function


Gets( )
It is used to read a set of alphanumeric character from stdin until a carriage return key is pressed and places a NULL in memory or returns.

Syntax

Gets(variable name);
Program:

#include<stdio.h>
Main()
{
Char  string[5];
Gets(string);
}


Puts( )
Puts( ) function used to send characters   as output that is to stdout from stored data until it run into the NULL when it sends a newline and returns.

Syntax

Puts(variable name);
Program:

#include<stdio.h>
Main()
{
Char string[5];
Gets(string);
Puts(string);
}


sscanf( )
This function is same as scanf function but the difference is that it read data stored in an array instead of  data entered by keyboard.

Syntax:

Sscanf(memory name, “control characters”, variable name);


Program:

#include,stdio.h>
Main()
{
Char string[5];
Int I,j;
Float a,b;
Gets(string);
Sscanf(string, “%d%d%f%f\n”, I, j, a, b);
}


Sprint( )
Like printf this function also use to enter data, but the difference is that sprint  write  formatted data in array istead of memory.

Syntax:

Sprint(memory name, “control character”, variable name);
Program:

#include<stdio.h.
Main()
{
Char string[5];
Int I,j;
Float a, b;
Gets(char);
Sprint(string, “%d%d%f%f\n”, I , j, a, b);
}


String handling functions


Strcmp( )
This function compares two strings character by character and returns -1 if ASCII value of first string is less then second string, 0 if AsCII value of both string is same, and 1 if ASCII  value of first string is greater then second string.
Syntax:

Strcmp(string1, string2);


Strcpy( )
This function is used to copy one string to another.

Syntax:

Strcpy(string1, string2);


Example: strcpy(“mouse”,”keyboard”);
This copies the string keyboard to mouse. Here , the current content of mouse is lost.

Strcat( )
This function is used to concatenate two strings. That is it appends one string at the end of other string.
Syntax:

Strcat(string1, string2);

Example: (“computer”, “concepts”);

Output will be
 computer concepts.

Strlen( )

This returns the number of characters in string.
Syntax: strlen(string);
Example: strlen(“computer”);
Output will be:
8

Strcmp( )
This function compares the first n character of two input strings.
Syntax: (string1, string2, n);
Example: strcmp(“elephant”,” lion”, 4);
It compares starting 4 strings and output will be as that of strcmp()  dissed.

Strncpy( )
This copies first n characters of second string to the first string.
Syntax: strncpy(string1, string2, n);
Example: strncpy(“sachin”, “tendulkar”,6);
Output will be
Tendul
And sachin string will lost.

Strncat( )
This appends the first n character of second string to the end of first string,
Syntax: strncat(string1,string2);
example: strncat(“system”, “software engg”, 8);
output will be:
 system software

strlwr( )
this function converts string into lower case.
Syntax: strlwr(string);
Example: strlwr(“COMPUTER”);
Output will be
computer

strupr( )
it converts string into upper case.
Syntax: strupr(string1);
Example: strupr(“computer”);
Output will be:
COMPUTER

Strchr( ) 
This function search for the character provided in string.it returns NULL if the desired character is not found in the string.
Syntax: strchr(string, desired_char);
Example: strchr(“computer”, t);
Since t is in the string search is successful.

Saturday, 11 February 2012

functions in C and C++

Function


Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C
Generally functioms are classified into standard functions and user defined functions. the standard  functions are also called library functions or built in functions.all standard functions,such as sqrt(), abs(), log(), sin(), pow() etc. are provided in the library of the fuctions.these are selective.but, most of the applications need other functions than those available in software.these functions must be written by the user(programmer),hence,the name user definedfunctions.


Definition of function
A function is a group of statements that is executed when it is called from some point of the program. The following is its format:
Defining a function means writing a actual code of a function which does a specific and identifiable task suppose you are defining a function which  compute the square of a given number. Then you have to write a set of instructions to do this there are different methods to define functions. The following two ways are are used in defining functions:-
NON ANSI style
ANSI style
The general form of function declaration in non ANSI style
Type_of_return_value name_of_function();
Example:


#include<stdio.h>
main()
{
Char ch, ct;
Char char_is();
Printf(“enter a character\n”);
Ch=getchar();
Ct=char_is(ch);
If(ct==’a’)
Printf(“it is an alphabet\n”);
else if(ct==’d’)
printf(“it is a digit\n”);
else if(ct==’x’)
printf(“it is a special character\n”);
}
Char char_is(cyar)
Char cyar;
{
If((cyar>=’a’ &&cyar<=’z’)||(cyar>=’a’ && cyar<=’z’))
Return(‘a’);
Else if(cyar>=’0’ &&cyar<=’9’)
Return(‘d’);
Else
Return(‘x’);
}


Output of the program
Enter the character
f
It is an alphabet

The ANSI style of function declaration differs with the non ANSI style only in the comments on the type of the variable received from the calling function.that is the pair of the parenthesis is empty in non-ANSI style, while the ANSI style contains the variables and their datatypes with in the parenthesis.
The general form of ANSI function  declaration  is as follows:
Type_of_return_value name_of_function( dt1 p1, dt2 p2 , dt3 p3………)
Where
dt1,dt2,dt3,……..dtn   are data types of (formal parameters)
p1,p2,p3,…..pn  are formal parameters.
Example:

#include<stdio.h>
#include<math.h>
main()
{
Int m,n,temp,I,rem;
Printf(“enter the values of m and n\n”)
Scanf(“%d %d”,&m, &n);
If(m>n)
{
temp=m;
m=n;
n=temp;
}
rem=(m%2);
if(rem==0)
m++;
printf(“prime numbers between %d and %d are …\n”,m,n);
for(i=m;i<=n;i+=2)
{
If(isprime(i))
Printf(“%d\n”,i);


}
}
Int isprime(int k)
{
int limit, td, remain;
remain=k%2;
if(remain)
{
Td=3;
Limit=sqrt(k);
While(remain &&(td<=limit))
{
Remainder=k%td;
td+ =2;
}
return(remain);
}


 Output of the program will be:
Enter the values of m and n
15
45
The prime numbers between m and n are……………
17
19
23
29
31
37
41
43

Parameter passing method:
There are two methods of parameters passing
1)call by value
2)call by reference


Call by value:
When the values of arguments are passed from the calling function to be called function, the values are copied into the called function, there is no change in the original values with in the calling function.

Call by reference:
In this method, the actual value are not passed, instead their addresses are passed. There is no copying of values since their memory locations are referenced.if any modification is made to the values in the called function, the original value get changed with in the calling function. Passing of addresses  require the knowledge of pointers