Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Programming in C & Data Structures

3.Explain the String Handling Functions with an example.

Answer:

String Handling Funtions:

 C Library has a number of string handling functions to carry out


string mani pulations.
 These functions are defined in the header file <string.h>

(i). Str cat():


Strcat() functions in used to concatenate two string it
concatenate the source string t the end of the destination string.

Syntax
Strcat [destination string source string];

Ex
String1=”VERY”.
String2=”GOOD”
Output
Strcat(strings1,string2);
VERY GOOD
(ii).Strcat():
 Strcpy() is ised to copy one string to another string.
 This fuction can works like assignment operator for the given
string.
Syntax
Strcpy(string1+,string2);
Output
“GOOD”

(iii).Strcmp():

 Strcmp() function is ised to compare two strings.


 This function can return a numerric value based on its oprations.
if str1 is same as str2 it return 0.
if str1 < str2 it return < 0
if str1 > str2 it return > 0

Syntax
Strcmp(string1,string2);

Ex
Char str1[]=”fresh”;
Char str2[]=”refresh”;
Int I,j,k;
I=strcmp(str1,”fresh”);
J=strcmp(str1,str2);
K=strcmp(str1,”f”);
Printf(“%d%d%d”,I,j,k);

(iv). Strlen():
Programming in C & Data Structures

Strlen() funtion is used to find the length of the given string.

Syntax
N=strlen(string1);
String1=”Welcome”;
N=strlen(string1);
N=7.

(v). Strrev():

Strrev() function is ised to find the reverse of the given string.

Syntax
String2=strrev(string1);

Ex
String1=”home”;
String2=strrev(string1);
String2=”emoh”.

(vi). Strupr():

Strupr() function is used to convert the given string from


lowercase to Uppercase .

Syntax
Str2=strupr(str1);

Ex
Str1=”Welcome”
Str2=strupr(str1);
Str2=”WELCOME”.

(vii). Strlwr():

Strlwr() function is ised to convert the given string from


uppercase to lowercase.
Syntax
Str2=strlwr(str1);

Ex
Str1=”PROGRAMMING IN C”;
Str2=strlwr(str1);
Str2=”programming in c”.

Example
#include<stdio.h>
#include<conio.h>
Void main()
{
Char str1[]=”welcome”
Char str2[]=”to cs”; char str3[20];int n;
Clrs();
Printf(“string manipulation \n”);
Programming in C & Data Structures

Printf(“concatenation of two strings:”,strcat(str1,str2));


Printf(“copy of string”,strcpy(str1,str2));
Printf(“comparison of two string”,strcmp(str1,str2));
Printf(“reverse of string”,strrev(str1));
Printf(“uppercase of string1”,strupr(str1));
Printf(“lowercase of string2”,strlwr(str2));
N=strlen(str1);
Printf(“length of string”,\n);
}

4.Explain in detail about function in C.

Answer:

 A function is a block of code that performs a specific


task.
 Function is classified aas one of the derived data type
in C.
Type Of Function:

 Library Function
 User Defined Function

Function

Library Function User Defined Function

Library Function:

 Library functions are not requried to written by the


programmers.
 But these are available in seporate file and it could be
include in appropriote place by the programmer.
 Function prototype and definition of these functions are
written in their header file.

EX

include<stdio.h>
 Printf,scanf functions are available in this leader file.

User Define Function:

 User define function can be defined by the program during


writing a program.
 Function are mode for code revseability and for saving time
and space.
 When C compiler compiles the program it will be executed
from main function.
Programming in C & Data Structures

 All other functions will be controlled by the main function.

Function Declaration or Prototype:

 Like the variable an array the function what must be


also declared before its called.
 The function declaration tells the compiler about the
name to call the function.

Syntax

Function type function name(argument list);

EX
 int add number(int a,int b);
 int add number(int,int);
 Both are the examples of vaild function.
 Parameter names are not important in function declaration only the
type in important.

Part of User Defined Function:

A function that is declare calling and define by the user is


called.

User Define Function:

Every user define`function has 3 part


 Function Definition
 Function call
 Function Declaration

Definition Of Function:

 Function definition is also known as function


implementation.
 A function should be Defined before it is used.

 A function has too path


 Function Header
 Function Boby

Syntax
function-type function name(argument list)
{
Local Variable declaration;
executeble Statent 1;
executeble Statent 2;
……………………………………………………
……………………………………………………
Return(expression);
}
Programming in C & Data Structures

Rules:

 Function header should not terminate with semicolon.


 List of argumant are optional
 If the function does not have any argument list empty paranthese is
must.
 Expression in the return Statement is optional.

EX
#include<stdio.h>
int add numbers(int a,int b);
int main()
{
Int n1,n2,sum;
Printf(“Enter two Numbers\n”);
Scanf(“%d%d”,&n1&n2);||function call sum =(add numbers(n1,n2);
Printf(“The sum is %d”,sum);
Return 0;
}
Int add numbers(int a;int b)||function definition)
{
int result;
int =a+b;
return ;
}

Function Returning Nothing:

If the fuction is not to return a value, we can declare that as the


function type.

Void

Syntax
Void function name(argument list)

Ex
Int factorial (int x)
{
int r=1;
if(r=1)
return 1;
else
r=x*factorial(x=1);
return r;
}

Function Calls:

Function can be called simply by using function name followed by


list of argument with in paranthese.
Programming in C & Data Structures

Syntax
Function name(argument list);

Rules:

 Function name should be same as used in function


declaration/definition.
 Date type inargument list must be matched.
 Onl one return value for calling funtion.

6.What is meant by Recursion? Write a C program to find the factorial of


a number using recursion.

Answer:

• Recursion is a function that call itself again and again.


• Normal function will be called by other function by its
name,but Recursion function will be called by itself as long as the
condition satisfied.
• Recursion is a special case of function.

Syntax

Return type recursion function(argument list)


{
statements
…………………………
…………………………
recursion function(argument list);
}

Types Of Recursion:
 Direct Recursion
 Indirect Recursion

Direct Recursion:
A function is said to be direct recursion if its called
directly by itself.

void recursion name()


{
………………………………………………
………………………………………………
recursion name();
}
int main()
Programming in C & Data Structures

{
……………………
recursion name();
}

EX
main()
{
printf(“tthis is direct recursion example \n”);
main();
}

Indirect Recursion:
A function is said to be indirect recursion if it call another
function and this new function calls the first calling function again.

int recursion_name 1()


{
……………………………………………………
……………………………………………………
recursion_name 2();
}

int recursion_name 2();


{
………………………………………………………
………………………………………………………
recursion_name 1();
}

EX

#include<stdio.h>
int factorial(int n)
{
if(n==0)
return 1;
else
return(n*factorial(n-1));
}
int main()
{
int num f;
printf(“Enter a number \n”);
scanf(“%d”,& num);
f = factorial(num);
printf(“factorial of %d = %d”num,f);
return 0;
}

Output
Enter a number Authors
5
factorial of 5 = 120
Love Devil

You might also like