Page 1 :
Object Oriented Concepts £ C++ Programming, , FUNCTIONS, , > Introduction: A function is a named group of statements developed to solve a sub-problem and, returns a value to other functions when it is called., > Types of functions, e There are two types of functions:, 1. Library functions, 2. User-defined functions, , Y Library functions: A standard library is a collection of pre-defined functions and other, programming elements, which are accessed through header files., e Header files are the files containing standard functions that our programs may use., e The header files should be written angled brackets and its functions are included into our programs, by #include directive., , Y User-defined functions: We can create our own functions or sub-programs to solve our, problem. Such functions are normally referred to as user-defined functions., e A user-defined function is a complete and independent program, which can be used (or, invoked) by the main program, or by the other sub-programs., ¢ The user-defined functions are written to perform definite calculations, after performing their task, they send back the result to the calling program or sub-program., , > Different header files, , ¥ iostream.h, © This header file contains C++ streams and i/o routines and are listed below., © open, close, get, getline, read, write, put, seekg, seekp, tellg, tellp, eof etc., , Y stdio.h, e This header file contains functions and macros to perform standard i/o operations., e When we include the header file iostream.h, the header file stdio.h is automatically included into, our program. The standard i/o functions of the header file stdio.h are listed below., e fopen, fclose, printf, scanf, fprintf, fscanf, fread, fwrite etc., , Y string.h, e This header file declares functions to manipulate strings and memory manipulation routines., e The functions contained in the header file string.h are listed below in the table., , , , , , , , , , , , , , , , Function Meaning, , strlen(s) It gives the no. of characters including spaces present in a string s., , streat(s1, s2) It concatenates the string s2 onto the end of the string s1., , strepy(sl, s2) It copies character string s2 to string sl. The sl must have enough, storage locations to hold s2.
Page 2 :
Object Oriented Concepts & C++ Programming, , , , stremp((s1,s2)==0) | It compares s1 and s2 and find out whether s1 equal s2, greater, stremp((s1,s2)>0) than s2 or s1 less than s2., , stremp((s1,s2)<0), strempi((s1,s2)==0) | It compares s1 and s2 ignoring case and find out whether s1 equal, strempi((s1,s2)>0) s2, greater than s2 or sl less than s2., , , , , , , , , , , , , , , , , , strempi((s1,s2)<0), , strrev(s) It converts a string into its reverse., , strupr(s) It converts a string s into upper case., , strlwr(s) It converts a string into lower case., v — stdlib.h, , ° This header file is used to declare conversion routines, search/sort routines and other miscellaneous, things. The functions are listed below., atoi, atol, atof, free, malloc, calloc, realloc etc., , ¥ iomanip.h, ee This header file contains functions and macros for I/O manipulators for creating parameterized, manipulations. The functions are listed., endl, setw, setfile, setprecision, hex, oct etc., , ¥ math.h, © This header file declares prototypes for the mathematical functions and error handlers., ¢ The functions that are used to perform mathematical calculations and conversions are listed below, , in the table., Function Meaning, © sqrt(x) Square Root of X, © pow(x, y) x raised to the power y, © sin(x) Sine of an angle x (measured in radians), © cos(x) Cosine of an angle x (measured in radians), © tan(x) Tangent of an angle x (measured in radians), © asin(x) Sin-1(x) where x (measured in radians), e =acos(x) Cos.i(x) where x (measured in radians), © exp(x) Exponential function of x (ex), e log(x) Logarithm of x, © = log 10(x) Logarithm of number x to base 10, © abs(x) Absolute value of integer number x, e = fabs(x) Absolute value of real number x, , > Character functions, , A character is any single character enclosed within single quotes., , Some functions accept a character as argument., , The argument is processed as an int by using its ASCII code., , To use these functions, the header file ctype.h should be included., , Any character enclosed within single quotes is called as a single character constant or simply, a, character constant.
Page 3 :
Object Oriented Concepts L C++ Programming, , e These functions will be of the form : int function-name(int character), , e Any character enclosed within single quotes is called as a single character constant or simply, a, character constant., , e Some functions that perform operations on characters are given below., , Function Meaning, e isalpha(c) It returns True if C is an uppercase letter and false if c is lowercase., © isdigit(c) It returns True if c is a digit (0 to 9) otherwise false, e isalnum(c) It returns True if c is digit from 0 through 9 or an alphabetic character, © islower(c) It returns True if c is a lowercase letter otherwise False, © isupper(c) It returns True if c is a uppercase letter otherwise False, © toupper(c) It converts c to uppercase letter, e = tolower(c) It converts c to lowercase letter., , > Inputting single character, ¢ We can input a character using the function get ()., e The general form is, char ch; or char ch;, cin =get(ch);, ch=cin.get(ch);, , > Outputting single character, e put () function is used to display single character., e The general form is cout.put (ch);, e Example: char ch;, ch=cin.get( );, cout.put (ch);, , Program to determine the type of character., #include<iostream.h>, #include<conio.h>, #include<ctype.h>, void main ( ), {, char ch;, clrser( );, cout<<"Type-in a character:”;, ch=cin.get();, if((ch>="A’ &&ch<="Z’)|\(ch>="a"’ &&ch<="z’)), cout<<"It is an albhabet”<<endl;, else, if(ch>="0" &&ch<="9"), cout<<"It is a digit”<<endl;, else, cout<<"It is a special character”<<endl;, getch( );, }
Page 4 :
Object Oriented Concepts L C++ Programming, , Program to convert a character from upper-case to lower case and vice versa., #include<iostream.h>, #include<conio.h>, #include<ctype.h>, void main( ), , {, char ch;, clrscr( );, cout<<"Type-in a character:”;, ch=cin.get();, if( isupper (ch) ), {, , , , ch= tolower (ch);, cout<<"The lower-case letter is”<<ch<<endl;, }, else, if( islower (ch) ), {, ch= toupper (ch);, cout<<"The upper-case letter is”<<ch<<endl;, }, else, cout<<"It is not an alphabet”<<endl;, getch();, }, , > String functions: A string is sequence of characters enclosed within double quotes., e String are manipulated as one-dimensional array of characters and terminated by null (*\0’), character., © C++ provides many functions to manipulate strings., e Touse these functions, the header file string.h should be included., , > Declaring a string variable, ¢ The general form to declare a string is:, char string _namef[size];, e String_ name is the name of the string variable, e Size is the number of characters in the string. The size helps the compiler to allocate required, number of memory locations to store the string., Example: char st{50];, This declaration reserves 50-bytes to store the characters of the string variable st., , > Initializing a string, e Like other variables, strings can also be initialized when they are declared., Example: char s[10]="Karnataka”;, e There are only 9 characters in the string. The null character (‘\0’) is automatically appended to the, end of the string., Example: char st[]={‘E’, ‘m’, ‘p’, ‘r’, ‘e’, ‘s’, ‘s’,’\0"};
Page 5 :
Object Oriented Concepts & C++ Programming, , The string is initialized character —wise. In this case, we must specify the null character., e If an entire string is initialized to a string-variable then the null character is automatically, appended to end-of-string., e If string is initialized by giving the characters, then we must explicitly specify the string, terminator. i.e., null character., , > Inputting a string: C++ provides the function getline( ) to read a string., e The general form is: cin.getline (string, size);, , Example :- cin.getline (st, 25);, getline( ) the function terminates reading characters on reading a newline character or when, number of characters read is equal to size., , e The newline character is read, but replaced by null character., , > Outputting a string: C++ provides the function write ( ) to output a string., e The general form is: cout.write (string, size);, Example:- cout.write (st, 25);, e write () function displays the specified number of characters., e This function does not stop displaying the characters when encountering the null character. As a, result, if the size specified is greater than the length of the string, it displays beyond the bounds of, the line., , Program to read a string and print the string., #include<iostream.h>, #include<conio.h>, #include<string.h>, void main( ), , {, char s[50];, int 1;, clrser( );, cout<<"enter the string:”;, cin.getline(s,50);, l=strlen(s);, cout<<"the given string is”;, cout.write(s,1);, getch();, , Some string manipulation functions are given below:, Y — strlen( ) function: This function returns the length of the string .i.e., the number of characters, present in the string, excluding the null character., , e The general form is variable=strlen(string);, , e Asstring of length ‘0° is called a null string., , Example:- |= strlen (“empress”); returns 7