Cunit III
Cunit III
1)String:-A String in C is nothing but a collection of characters in a linear sequence. ‘C’ always
treats a string a single data even though it contains whitespaces. A single character is defined
using single quote representation. A string is represented using double quote marks.
2)String Handling Functions:- Following are some of the useful string handling functions
supported by C.
1. strlen( )
2. strcpy ( )
3. strcat( )
4. strcmp ( )
5. strupr( )
6. strlwr( )
These functions are defined in string.h header file. Hence you need to include this header file
whenever you use these string handling functions in your program.
All these functions take either character pointer or character arrays as arguments.
1. strlen( ):-strlen( ) function returns the length of the string. strlen() function returns
integer value.
2. strcpy( ):-strcpy( ) function is used to copy one string to another. The Destination_String
should be a variable and Source_String can either be a string constant or a variable.
Syntax: strcpy(Destination_String,Source_String);
4. strcmp( ):-strcmp( ) function is use two compare two strings. strcmp() function does a
case sensitive comparison between two strings. The Destination_String and
Source_String can either be a string constant or a variable.
Syntax:int strcmp(string1, string2);
3)Function:- A large C program is divided into basic building blocks called C function. C
function contains set of instructions enclosed by “{ }” which performs specific operation.
Function declaration or prototype:- - This informs compiler about the function name,
function parameters and return value’s data type.
float m, n ;
printf ( "\nEnter some number for finding square \n");
scanf ( "%f", &m ) ;
n = square ( m ) ;
printf ( "\n Square of the given number %f is %f",m,n );
}
float square ( float x )
{
float p ;
p=x*x;
return ( p ) ;
}
Output:-
4)function call:-There are two ways that a C function can be called from a program. They are,
1. Call by value
2. Call by reference
1. Call by value:-In call by value method, the value of the variable is passed to the function as
parameter.The value of the actual parameter can not be modified by formal parameter.Different
Memory is allocated for both actual and formal parameters. Because, value of actual parameter is
copied to formal parameter.
#include<stdio.h>
void swap(int a, int b);
int main()
{
int m = 22, n = 44;
printf(" values before swap m = %d \nand n = %d", m, n);
swap(m, n);
}
Output:-
2. Call by reference:-In call by reference method, the address of the variable is passed to the
function as parameter. The value of the actual parameter can be modified by formal parameter.
Same memory is used for both actual and formal parameters since only address is used by both
parameters.
#include<stdio.h>
void swap(int *a, int *b);
int main()
{
Output:-
and n = 44
values after swap a = 44
and b = 22
6 Strings and Functions
Recursive functions are very useful to solve many mathematical problems, such as calculating the
factorial of a number, generating Fibonacci series, etc.
Number Factorial
The following example calculates the factorial of a given number using a recursive function –
#include <stdio.h>
int factorial(unsigned int i)
{
if(i <= 1)
{
return 1;
}
return i * factorial(i - 1);
}
int main() {
int i = 5;
printf("Factorial of %d is %d\n", i, factorial(i));
return 0;
}
When the above code is compiled and executed, it produces the following result –
Factorial of 5 is 120
7 Strings and Functions