0% found this document useful (0 votes)
48 views14 pages

Inbuilt Function

The document discusses various commonly used inbuilt functions in C++. It lists functions for input/output, character manipulation, strings, mathematics and other operations. It provides the function header, description and examples for standard functions like abs(), strcpy(), strlen(), isalpha() etc that are used to perform tasks like copying strings, comparing strings, calculating lengths and absolute values.

Uploaded by

Naseeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views14 pages

Inbuilt Function

The document discusses various commonly used inbuilt functions in C++. It lists functions for input/output, character manipulation, strings, mathematics and other operations. It provides the function header, description and examples for standard functions like abs(), strcpy(), strlen(), isalpha() etc that are used to perform tasks like copying strings, comparing strings, calculating lengths and absolute values.

Uploaded by

Naseeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

9/13/2018 Inbuilt Function | KVSeContents.

in

Inbuilt Function
List of Some Commonly used inbuilt Functions in C++
Standard input/output functions
stdio.h
gets (), puts ()
Character Functions
ctype.h
isalnum (), isalpha (),isdigit (), islower (),isupper (), tolower
(),toupper ()
String Functions
string.h
strcpy (), strcat (),strlen (), strcmp (),strcmpi (), strrev
(),strlen (), strupr (),strlwr ()
iomanip.h
setw(),

Mathematical Functions
math.h
abs (), pow (), sgrt (),sin (), cos (), abs ()
Other Functions
stdlib.h
randomize (), random (),itoa (), atoi ()
Using Library Functions

Header File :ctype.h


int isalnum(int c);
Description:
The function returns nonzero if c is any of:
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 1/14
9/13/2018 Inbuilt Function | KVSeContents.in

a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
o 1 2 3 4 5 6 7 8 9
Return Value
The function returns nonzero if c is alphanumeric otherwise this
will return zero which will be equivalent to false.
Example
#include <iostream.h>
#include<ctype.h>
int main() {
if( isalnum( '#' ) )
{
cout<< "Character # is not alphanumeric\n" ;
}
if( isalnum( 'A' ) )
{
cout<< "Character A is alphanumeric\n" ;
}
return 0;
}
It will produce following result:
Character A is alphanumeric
int isalpha(int c);
Description:
The function returns nonzero if c is any of:
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Return Value
The function returns nonzero if c is just alpha otherwise this
will return zero which will be equivalent to false.
Example
#include <iostream.h>
#include<ctype.h>
int main() {
if( isalpha( '1' ) )
{
cout<< "Character 1 is not alpha\n" ;
}
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 2/14
9/13/2018 Inbuilt Function | KVSeContents.in

if( isalpha( 'A' ) )


{
cout<< "Character A is alpha\n";
}
return 0;
}
It will produce following result:
Character A is alpha
int isdigit(int c);
Description:
The function returns nonzero if c is any of:
0 1 2 3 4 5 6 7 8 9
Return Value
The function returns nonzero if c is a digit otherwise this will
return zero which will be equivalent to false.

Example
#include <iostream.h>
#include<ctype.h>
int main() {
if( isdigit( '1' ) )
{
cout<< "Character 1 is a digit\n" ;
}
if( isdigit( 'A' ) )
{
cout<< "Character A is digit\n" ;
}
return 0;
}
It will produce following result:
Character 1 is a digit
int islower(int c);
Description:
The function returns nonzero if c is any of:
a b c d e f g h i j k l m n o p q r s t u v w x y z
Return Value
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 3/14
9/13/2018 Inbuilt Function | KVSeContents.in

The function returns nonzero if c is lower case otherwise this


will return zero which will be equivalent to false.
Example
#include <iostream.h>
#include<ctype.h>
int main() {
if( islower( 'a' ) )
{
cout<<"Character a is lower case\n";
}
if( islower( 'A' ) )
{
cout<< "Character A is lower case\n";
}
return 0;
}
It will produce following result:
Character a is lower case
int isupper(int c);
Description:
The function returns nonzero if c is any of:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Return Value
The function returns nonzero if c is upper case otherwise this
will return zero which will be equivalent to false.
Example
#include <iostream.h>
#include<ctype.h>
int main() {
if( isupper( 'a' ) )
{
cout<< "Character a is upper case\n" ;
}
if( isupper( 'A' ) )
{
cout<<"Character A is upper case\n";
}
return 0;
}
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 4/14
9/13/2018 Inbuilt Function | KVSeContents.in

It will produce following result:


Character A is upper case
int tolower(int c);
Description:
The function returns the corresponding lowercase letter.
Example
#include <iostream.h>
#include<ctype.h>
int main() {
cout<< "Lower case of T is \n"<<tolower('T');
return 0;
}
It will produce following result:
Lower case of T is t
int toupper(int c);
Description:
The function returns the corresponding uppercase letter.

Example
#include <iostream.h>
#include<ctype.h>
int main() {
cout<< "Upper case of t is \n"<<toupper('t');
return 0;
}
It will produce following result:
Upper case of t is T
C++ Library Functions Header File :string.h
char *strcpy (char *dest, char *src);
Description:
The strcpy function copies characters from src to dest including
the terminating null character.
Return Value
The strcpy function returns dest.
Example
#include <iostream.h>
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 5/14
9/13/2018 Inbuilt Function | KVSeContents.in

#include <string.h>
int main() {
char src[20];
char dest[20];
strcpy(src, "Hello");
strcpy(dest, src");
// Will copy the contents from src array to dest array
cout<<"dest ="<<dest;
return 0;
}
It will produce following result:
dest = Hello
char *strcat(char *dest, const char *src);
Description:
The strcat function concatenates or appends src to dest. All
characters from src are copied including the terminating null
character.
Return Value
The strcat function returns dest. Example
#include <string.h>
#include <iostream.h>
int main()
{
char src[20];
char dest[20];
strcpy(src, "Ayan");
strcpy(dest, "Sharma");
cout<<"Concatenated String = \n"<<strcat(src,dest);
return 0;

http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 6/14
9/13/2018 Inbuilt Function | KVSeContents.in

}
It will produce following result:
Concatenated String = AyanSharma
int strlen(char *src );
Description:
The strlen function calculates the length, in bytes, of src. This
calculation does not include the null terminating character.

Example
#include <string.h>
#include <iostream.h>
int main()
{
char src[20];
strcpy(src, "Ambari");
cout<<"Length of string :"<<src<< " is "<<strlen(src );
return 0;

}
It will proiduce following result:
Length of string Ambari is 6
[email protected] < >'; document.write( '' );
document.write( addy_text77487 ); document.write( '<\/a>' ); //-->
;;

int strcmp(char *string1, char *string2);


Description:
The strcmp function compares the contents of string1 and string2
and returns a value indicating their relationship.
Return Value
if Return value if < 0 then it indicates string1 is less than
string2
if Return value if > 0 then it indicates string2 is less than
string1
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 7/14
9/13/2018 Inbuilt Function | KVSeContents.in

if Return value if = 0 then it indicates string1 is equal to


string1
Example
#include <string.h>
#include <iostream.h>
int main()
{
char one[20];
char two[20];
strcpy(one, "Ayan");
strcpy(two, "Sharma");
cout<<"Return Value is : \n"<<strcmp( one, two);
strcpy(one, "Sharma");
strcpy(two, "Ayan");
cout<<"Return Value is : \n"<<strcmp( one, two);
strcpy(one, "Ayan");
strcpy(two, "Ayan");
cout<<"Return Value is : \n"<<strcmp( one, two);
return 0;
}
It will produce following result:
Return Value is : -18
Return Value is : 18
Return Value is : 0
Declaration: char *strupr(char *s);
Description:
strupr convert lowercase letters (a to z) in string s to
uppercase (A to Z).
#include <iostream.h>
#include <string.h>
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 8/14
9/13/2018 Inbuilt Function | KVSeContents.in

int main(void)
{
char *s = "abcde";
/* converts string to upper case characters */
cout<<endl<<s;
return 0;
}
Output
ABCDE
Declaration: char *strrev(char *s);
Description:
strrev changes all characters in a string to reverse order,
except the terminating null character.For example, it would change
AYAN\0
to
NAYA\0
#include<string.h>
#include<iostream.h>
int main(void)
{
char *s = "AYAN";
cout<<"After strrev(): "<<strrev(s);
return 0;
}
Output
NAYA
C++ Library Functions Header File :math.h
Declaration :- abs(int n);
Description:
abs is not a function but is a macro and is used for calculating
absolute value of a number.
#include <iostream.h>
#include <math.h>

http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 9/14
9/13/2018 Inbuilt Function | KVSeContents.in

int main()
{
int n, result;
cout<<"Enter an integer to calculate it's absolute value\n";
cin>>n;
result = abs(n);
cout<<"Absolute value of "<<n<< "is "<<result;
return 0;
}
Output
Enter an integer to calculate it's absolute value -5
Absolute value of -5 is 5
Declaration :- double pow(double, double);
Description:
pow function returns x raise to the power y where x and y are
variables of double data type.
#include <iostream.h>
#include <math.h>
int main()
{
int m,n, result;
cout<<"Enter two integers to calculate power\n";
cin>>m>>n;
result = pow(m,n);
cout<<"Result is "<<result;
return 0;
}
Output
Enter two integers to calculate power 3 4
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 10/14
9/13/2018 Inbuilt Function | KVSeContents.in

Result is 81
Declaration :- double sqrt(double);
Description:
sqrt function returns square root of a number.

#include <iostream.h>
#include <math.h>
int main()
{
double n, result;
cout<<"Enter an integer to calculate it's square root \n";
cin>>n;
result = sqrt(n);
cout<<"Result is "<<result;
return 0;
}
Output
Enter an integer to calculate it's square root 9
Result is 3
Declaration: double sin(double);
Description:
Sin function returns sine of an angle(in radian).

#include <iostream.h>
#include <math.h>
int main()
{
double result, x = 0.5;
result = sin(x);
cout<<"sin(x ) of 0.5 is "<<result;
http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 11/14
9/13/2018 Inbuilt Function | KVSeContents.in

return 0;
}
Output
sin(x) of 0.5 is 0.479426
Declaration: double cos(double);
Description:
Cos function returns cosine of an angle(in radian).
1 radian = 57.2958(approximately).
#include <iostream.h>
#include <math.h>
int main()
{
double result, x = 0.5;
result = cos(x);
cout<<"cos(x) of 0.5 is "<<result;
return 0;
}
Output
cos(x) of 0.5 is 0.877583
C++ Library Functions Header File :stdlib.h
Declaration: int random(int n);
Description:
random return a random number between 0 to (n-1)
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
/* prints a random number in the range 0 to 9 */
int main(void)

http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 12/14
9/13/2018 Inbuilt Function | KVSeContents.in

{
randomize();
cout<<"Random number from 0-9 range:\n"<<random(10);
return 0;

}
Declaration: void randomize(void);
Description:
randomize initializes random number generator
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
/* prints 3 random number in the range 0 to 9 */
int main(void)
{
randomize();
cout<<"3 random numbers from 0-9 range:\n";
for(int i=0;i<3;i++)
cout<<random (10)<<endl;
return 0;
}
Declaration:
char*itoa(int value,char *string,int radix);
Description: itoa converts an integer to a string
#include <iostream.h>
#include <stdlib.h>
int main(void)
{
int number = 444;
char string[5];

http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 13/14
9/13/2018 Inbuilt Function | KVSeContents.in

itoa(number, string, 10);


cout<<"integer = "<<number<<"string ="<<string;
return 0;
}
Output
integer 444 string 444
Declaration: int atoi(const char *s);
Description: Macro that converts string to integer
#include <stdlib.h>
#include <iostream.h>
int main(void)
{
int n;
char *str = "112";
n = atoi(str);
cout<<"string = "<<str<<"integer ="<<n;
return 0;
}
Output
string 112 integer 112

http://www.kvsecontents.in/index.php/inbuilt-functions?tmpl=component&print=1&page= 14/14

You might also like