int i,l=0;
char string[10];
printf(“Enter any string ”);
scanf(“%s”,string);
l=strlen(string);
printf(“Length of %s is %d”,string,l);
}
Output:
Enter any string Naveen Length of Naveen is 6
2. strcpy(): This function copies the contents of one string into another. The base addresses of the source and target strings should be supplied to this function.
Syntax:
strcpy(string1, string2);
Example:
#include<stdio.h>
#include<string.h>
void main() {
char string1[10],string2[10];
printf(“Enter first string ”);
scanf(“%s”,string2);
strcpy(string1,string2);
printf(“Copied string is %s”,string1);
}
Output:
Enter first string Naveen Copied string is Naveen
Prepared by Mr. K. G. Page 58 3. strcmp(): This function is used to compares two strings to find out whether they are same or different. If the two strings are equal strcmp() returns a value zero otherwise it returns the numeric difference between the ASCII value of the first non-matching characters.
Syntax:
strcmp(string1, string2);
Example:
#include<stdio.h>
#include<string.h>
void main() {
int c;
char string1[10],string2[10];
printf(“Enter first string ”);
scanf(“%s”,string1);
printf(“Enter second string ”);
scanf(“%s”,string2);
c=strcmp(string1,string2);
if(c==0) {
printf(“Both strings are same”);
} else {
printf(“Both strings are different”);
} }
Output:
Enter first string Naveen Enter second string Dharam Both strings are different
4. strcat(): The strcat() function is used to join strings. When two strings are joined, it is referred as concatenation of strings. The string2 is concatenate at the end of string1. Syntax:
strcat(string1, string2);
Example:
#include<stdio.h>
#include<string.h>
void main() {
char string1[20],string2[10];
printf(“Enter first string ”);
scanf(“%s”,string1);
printf(“Enter second string ”);
scanf(“%s”,string2);
strcat(string1,string2);
printf(“Joined string is %s”,string1);
}
Output:
Enter first string Naveen Enter second string Sahu
Prepared by Mr. K. G. Page 59 Joined string is NaveenSahu
3.5 Additional String Handling Functions: Some C compilers will accept the following string handling functions which are available in header files string.h and ctype.h.
Function Purpose Example Result
strupr() To convert all alphabets in a string to
strrev() To reverse a string. strrev(“Delhi”) ihleD
strncmp() To compare the first n characters of two strings.
m=strncmp(“Delhi”, “Dipak”, 2) m=-4 strcmpi() To compare two strings with case
insensitive.
m=strcmpi(“Delhi”, “DELHI”) m=0 strncat() To join specific number of letters to
another string.
strncat(“New”, “Delhi”, 3) NewDel atoi() To convert a string of digits to the
integer value.
atoi(“1234”) 1234
Character Handling Functions:
Function Purpose Example Result
isupper() To test if a character is an upper case
isalpha() To test if a character is an alphabet. isalpha(‘K’) isalpha(‘+’) isdigit() To test if a character is a number. isdigit(‘5’)
isdigit(‘a’)
1 0 isxdigit() To test if a character is a hexa-decimal
number.
Prepared by Mr. K. G. Page 60 Unit-4
User Defined Functions
4.1 Functions: Functions are subprograms which are used to compute a value or perform a task. They cannot be run independently and are always called by the main() program or by some other function.
There are two kinds of functions:
1. Library or built-in functions: Library or built-in functions are used to perform standard operations, e.g. square root of a number sqrt(x), absolute value fabs(x), scanf(), printf() and so on. These functions are available along with the compiler and are used along with the required header files such as math.h, stdio.h, string.h and so on at the beginning of the program.
2. User-defined functions: User-defined functions are self-contained blocks of statements which are written by the user (programmer) to compute a value or perform a task. They are called by the main() program or by some other function.
Uses of Functions:
(a) Functions are very much useful when a block of statements has to be written/ executed again and again.
(b) Functions are useful when the program size is too large or complex. Functions are called to perform each task sequentially from the main program. It is like a top-down modular programming technique to solve a problem.
(c) Functions are used to reduce the difficulties during debugging a program.
4.2 Function Definition: A function definition includes the following elements:
1. Return type/function type 2. Function name
3. List of arguments/parameters 4. Local variable declaration 5. Function statements 6. A return statement
All the six elements are grouped into two parts:
Function header (first three elements)
Function body (last three elements) A general form of function definition is:
return_type function_name(argument list) {
local variable declaration;
executable statement1;
executable statement2;
--- --- return statement;
}
The first line return_type function_name(argument list) is known as function header and the statements within the opening and closing braces constitute the function body, which is compound statement.
Prepared by Mr. K. G. Page 61 Example:
int add(int a, int b) { int c;
c=a+b;
return(c);
}
4.3 Function Declaration or Function Prototype: Function declaration are usually written at the beginning of a program, a head of program defined functions. It is terminated by semicolon. Function declaration consists of four parts.
1. Return type/function type 2. Function name
3. List of arguments/parameters 4. Terminating semicolon The general form is:
return_type function_name(argument list);
This is very similar to the function header line except the terminating semicolon.
For example:
int add(int a, int b);
4.4 Function Call: A function call is specified by the function name followed by a list of arguments, if any, enclosed in parentheses and terminated by a semicolon.
Example:
sum(x, y) // do not return any value z=sum(x, y) // will return value to z
Actual and Formal Arguments: Passing of values between the main program and the function takes place through arguments.
The arguments listed in the function calling statement are referred to as actual arguments.
They are the actual value passed to a function to compute a value or to perform a task. Consider a function call statement with actual arguments as given below.
add(x, y);
Actual arguments
The arguments used in the function declaration are referred as formal arguments. They are simply formal variables that accept or receive the values supplied by the calling program. Consider a function declaration with formal arguments as given below.
void add(int a, int b)
Formal arguments
Note that the number of actual and formal arguments and their data type should match.
Difference between actual and formal arguments
Actual arguments Formal arguments
1. Actual argument is specified in function call. It is specified on function definition.
2. It is also called real arguments or variables. It is also called dummy or parametric arguments or variables.
Prepared by Mr. K. G. Page 62 3. Data type should not be used with actual
arguments.
Data type must be used with formal arguments.
4. Actual arguments replace the formal arguments.
Formal arguments do not replace the actual arguments.
5. Example: Consider the following function call void main()
{
int a, y;
--- ---
add(x, y); // actual arguments ---
--- }
Example: Consider the following function declaration
void add(int a, int b) // formal arguments {
int c;
c=a+b;
printf(“sum=%d”,c);
}
Rules to Call a Function: The following rules are used to call a function in a C program.
1. A function has statement block which is called by the main() or any other function.
2. When the data type in a function declaration is omitted, the function will return a value of the type integer.
3. The data type of the formal argument may be declared in the next line which follows the function declaration statement. Consider the following example
big(a, b) int a, b;
{
if(a>b) return(a);
else
return(b);
}
Note that the data type of the function is not mentioned. So it returns an integer value to the calling program.
4. A function can return a value at any stage of its execution by using more than one return statement.
5. A function can also be called by another function.
6. The return statement is omitted if it does not return a value directly to the calling program.
7. Argument list (actual and formal) listed can be omitted, but not the parentheses ( ) following the function name.
4.5 Category of Functions: A function depending on whether arguments are presents or not and whether a value is returned or not, may belong to one of the following categories.