Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Unit C, Chapter-12 Functions

Computer notes of chap 12 : functions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Unit C, Chapter-12 Functions

Computer notes of chap 12 : functions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Page 1 Chapter 12 Functions

UNIT C
CHAPTER 12
FUNCTIONS

INTRODUCTION
In all our earlier programs, we were used only main() function to solve small problems. If the problem involves
a lengthy and complex algorithm, naturally, the length of the main() is also increase and cannot be managed
easily. Large programs can be made manageable by dividing it into smaller sub-programs or modules called
functions.

Defn: A function is a self contained block of independent statements designed to perform a specific task and
return a single value.

Two types of functions.


a) Built-in / Library functions
b) User-defined functions

a) Built-in functions /Library functions: A standard library it has a collection of predefined functions
which are accessed through header files.
OR
Are those which are already created by C++ compiler.
Ex: clrscr(), getch() cout(), cin(), strlen(), strcat(), isalpha(), toupper() and so on.

b) User-defined functions (UDF’s): Is a complete and independent programs, which are created by the
user to solve their own problem.
Ex: add(), sub(), multi(), largest(), main() and so on.

Header file:
Are the files containing standard functions that our program may use.

Types of header files


1. stdio.h
2. iostream.h
3. conio.h
4. string.h
5. stdlib.h
6. iomanip.h
7. math.h

1. stdio.h
It contains functions and macros to perform standard input output operations.
fgets() fputc() fscanf()
fwrite() scanf() printf()
fclose() getchar() putchar()

2. iostream.h
It contains C++ streams and I/O routines
open() close() get()
getline() read() write()

Prepared by: VIJAYAKUMAR M.Sc, B.Ed RJS PU COLLEGE


Page 2 Chapter 12 Functions

seekg() seekp() tellg()

3. conio.h
It contains functions to display I/O on display screen
clrscr()
getch()

4. string.h
It contains functions to manipulate strings and memory manipulation routines.
strlen() strcat() strcmp()
strcpy() strstr() strrev()
strupr() strlwr() memcpy()

5. stdlib.h
Used to declare conversion routines
free() malloc()
calloc() realloc()

6. iomanip.h
It contains functions and macros for I/O manipulators
setfile() endl setw()
setprecision() doc Hex

7. math.h
It contains list of mathematical functions
sqrt() sin() tan()
pow() exp() ceil()
floor() asin() acos()

STRINGS (CHARACTER ARRAY)

INTRODUCTION:
In early day’s, computers are used for process numerical type of data. Today, computers are frequently used for
processing non-numerical type of data such as character data. So, string helps us to discuss how such data are
stored and processed by the computer.

String Defn: String is a collection of character terminated or ending by NULL character ( “\0”).

DECLARATION OF STRING (2 marks)

Syntax:
<Char> <String-name> [size];

Char: Is a character data type.


String_name: Is the name given to the string.
Size: Indicate No. of characters in the string

Ex: char name[12];


Totally 12 locations are allocated to store string including NULL character.
name [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
P r o G r a M m i n g \0

Prepared by: VIJAYAKUMAR M.Sc, B.Ed RJS PU COLLEGE


Page 3 Chapter 12 Functions

NOTE: Individual character of the string is accessed by using index value.


name[0] refers to first character, ie P
name[4] refers to fifth character, ie r

NOTE: The C++ compiler will append Null character ‘\0’ at the end of each string to indicate ending of each
string

STRING INITILIZATION:
Assign each character in a string, when they are declared in the program
Syntax:
<Char> <string-name> [size] = {initializer list};

Case 1: Group of characters enclosed between pair of double quotes


Ex: char str[12] = “programming”;
Here, str is a string variable of size 12. It can store maximum 12 characters including NULL
character

Case 2: Group of characters including NULL (\0) character separated by comma and enclosed between a pair
of single quotes
Ex: char str[12] = {‘p’ , ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘m’, ‘i’, ‘n’, ‘g’, '\0'};
Here, str is a string variable of size 12. It can store maximum 12 characters including NULL
character.

Inputting a single character


cin.get() is a function to input a single character
GS
char ch;
ch=cin.get();

Outputting a single character


cout.put() is a function to output a single character
GS
char ch;
ch=cin.get();
cout.put(ch);

Inputting a string
cin.getline() Is a string input function used to accept string in character by character basis until it finds
newline.
General syntax
cin.getline(string, size);
Ex:
char name[20];
cin.getline(name,20);

Outputting a string
cout.write() is a string function used to display string character by character basis until it finds null (\0)
character.
GS
cout.write(string ,size);
Ex:
char name[20]=”C++ Programming”;
cout.write(name,10);

Prepared by: VIJAYAKUMAR M.Sc, B.Ed RJS PU COLLEGE


Page 4 Chapter 12 Functions

STRING OPERATIONS:
The various operations can be performed on the strings are
1) To find the Length of string. strlen()
2) Concatenation of two strings. strcat()
3) Copy one string to another string. strcpy()
4) Comparison of two strings. strcmp()
5) Finding substring. strstr()

1) Find the length of string (Strlen( )):


It counts the number of characters in a string excluding NULL (\0) character.
Syntax: int n;
n = strlen(name);
Example
char name[20] = “NEWEXPERTCOLLEGE”;
int n;
n = strlen(name);
cout<<“The length of the string is”<< n;
Output: The length of the string is 16.

2) Concatenation of two strings ( Strcat( ) ):


Combines the content of one string at the end of another string.
Syntax: strcat(str1, str2);
The content of string2 is added at the end of string1.
Example
char str1[8] = “NEWEXPERT”;
char str2[8] = “COLLEGE”;
strcat(str1, str2);
cout<<“The str1 string after the concatenation is”<< str1;

Output: The str1 string after the concatenation is NEWEXPERTCOLLEGE

NOTE: Concatenation means combine

3) strcpy( ) function:
It copies the content of one string to another string.

Syntax: strcpy( str1, str2);


Copies the content of str2 to str1.
Example
char str1[20] ;
strcpy(str1,”COLLEGE”);
cout<<“The str1 string after the copy is”<< str1;

Output: The str1 string after the copy is COLLEGE

4) strcmp( ) Function:
Used to compare two strings for equality. Comparison is done by character by character basis.
Syntax: strcmp(str1, str2);

Prepared by: VIJAYAKUMAR M.Sc, B.Ed RJS PU COLLEGE


Page 5 Chapter 12 Functions

Example

char str1[‘ ‘] = “IPUC”;


char str2[‘ ‘] = “IPUC”;
n = strcmp(str1,str2);
cout<<“The return value of strcmp ( ) is”<<n;
output: The return value of strcmp ( ) is 0

Note: If both the strings are equal, then strcmp( ) returns 0.

The possible return values of strcmp( ) function is as follows;


 If string1 and string2 are equal, it returns 0 values.
 If the string1 is greater than string2, it returns positive value
 If the string1 is smaller than string2, it returns negative value.

Note: To use all the string functions in C++, user must include string.h library file in the beginning of program.

String Handling Functions:


The string header file ‘string.h’ library supports a large number of string handling functions. They are as
follows;
strlen( ) Calculates the length of the string
strcat( ) Combines the content of two strings
strcmp( ) Comparison of two strings
strcpy( ) Copy the content of one string to another string
strrev( ) Reverse the characters in a string
strupr( ) Converts characters from lower case to upper case
strlwr( ) Converts characters from upper case to lower case
strstr( ) Finds one string within another string

strlwr( ) function and strupr( ) function


Strlwr( ) Function Strupr( ) Function
Converts upper case characters in a string to lower Converts lower case characters in a string to upper
case case
Syntax: strlwr(string); Syntax: strupr(string);
Ex: char str[10]= “NEWEXPERT”; Ex: char str[10]=”newexpert”;
strlwr(“str”) strupr(“str”);

Output: newexpert Output: NEWEXPERT

Strrev( ) Function:
Reverse all the characters in a string.
Syntax: strrev(string);
Ex: strrev(“ABCD”);
Output: DCBA

Character Manipulation Functions.( <ctype.h>)


It is used to manipulate a single character. Two types of character manipulation function.
a) Classification function: Name begins with the prefix is, and are used to determine the type of
character.

Prepared by: VIJAYAKUMAR M.Sc, B.Ed RJS PU COLLEGE


Page 6 Chapter 12 Functions

b) Conversion function: Name begin with the prefix to, and are used to convert one type of character to
another.

Classification functions Conversion functions


Function Description Function Description
isalpha( ) Returns a true value if the input is
A-Z or a-z letters toascii( ) Converts the character to ASCII format
isalphanum() Returns a true value if the input is
A-Z or a-z letters or 0-9
isdigit( ) Returns a true value if the input is tolower( ) Converts the characters to lower case.
0 to 9
isspace( ) Returns a true value if there is
space between characters
ispunct() Returns a true value if it is toupper ( ) Converts the characters to upper case.
punctuation character
. , * & % /-_@ etc.
isupper( ) Returns a true value if the input is
capital A-Z
islower( ) Returns a true value if the input is
small a-z toacsii() Converts all the character to ascii value
isascii() Returns a non-zero value if the
input is acsii value of characters

Note: To use all the character functions in C++, user must include ctype.h library file in the beginning of
program.
Difference between character and a string
Character String
Characters are enclosed within pair of single Strings are enclosed within pair of double
quotes quotes.
char str[10]= ‘A’; char str[10]=”NEWEXPERT”;
Declared using char keyword Declared using char keyword
<ctype.h> header file is used to manipulate <string.h> header file is used to manipulate
character. string

Difference between strcpy( ) and strcat( ):


strcpy( ) strcat( )
It is used to copy the content of one string to It is used to add the content of one string at the
another string. end of another string.
It is similar to string assignment. It is similar to appending more text to the
existing string.

Prepared by: VIJAYAKUMAR M.Sc, B.Ed RJS PU COLLEGE


Page 7 Chapter 12 Functions

Write a ‘C’ program to determine whether the string is a palindrome.


#include<iostream.h>
#include<conio.h>
#include <string.h>
main( )
{
char s[100], r[100];
clrscr();
cout<<”Enter the string”;
cin.getline(s, 100);
strcpy(r, s);
strrev(r);
if(strcmp(s, r)==0)
cout<<”String is a palindrome”;
else
cout<<”string is not palindrome”;
getch();
}

NOTE:
Palindrome: Read a string from left to right & right to left it will give same meaning
Ex: MALAYALAM, MADAM, LIRIL & etc.

---*******---

Prepared by: VIJAYAKUMAR M.Sc, B.Ed RJS PU COLLEGE

You might also like