0% found this document useful (0 votes)
3 views8 pages

C - 5 Mark

The document outlines various string handling functions in C, including strlen, strcpy, strncpy, strlwr, strupr, and strcat, detailing their syntax and examples. It also explains recursion, its components (base case and recursive case), and differentiates between direct and indirect recursion with examples. Additionally, it compares call by value and call by reference, providing examples to illustrate the differences in how function arguments are passed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

C - 5 Mark

The document outlines various string handling functions in C, including strlen, strcpy, strncpy, strlwr, strupr, and strcat, detailing their syntax and examples. It also explains recursion, its components (base case and recursive case), and differentiates between direct and indirect recursion with examples. Additionally, it compares call by value and call by reference, providing examples to illustrate the differences in how function arguments are passed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

String Standard Functions

C supports a large number of string handling library functions. String manipulation


functions

are given in string.h header file.

1. strlen( )

This function returns the number of characters in a string.

Syntax:

n =strlen (string);

Where n is an integer variable which receives the value of the length of the string.

Example:- int n;

char str1* + = “mgu”;

n = strlen(str1); ------- here n will get 3

2. strcpy( )

This function copies the contents of one string into another.

Syntax: strcpy (string2, string1);

Where string1 is the source string, string2 is the destination string i.e., string1 is
copied to

string2.

Example:- char source* + = “baselios”;

char target[20];

strcpy (target, source);

Here the string “baselios” is copied to target.


3. strncpy()

This function copies specified length of characters from the source to destination
string.

Syntax: strncpy (s2, s1, n);

Where s1 is the source string, s2 is the destination string. The leftmost n

characters are copied from s1 into s2.

Example:

char s1* +=”wonderful”;

char s2* +=”beautiful”;

strncpy(s2,s1,6);

printf(“%s”, s2);

it will print wonderful

4. strlwr( )

It converts a string to lowercase.

Syntax:

strlwr(string);

Example:- strlwr(“HELLO”); here the output will be hello

5. strupr()

It converts a string to upper case.

Syntax:

strupr(string);

Example:- strupr(“hello”); here the output will be HELLO

6. strcat( )

This function joins(concatenates) two strings together. That is this function appends
the target string to the source string.
Syntax: strcat (string1, string2);

Example:- char s1* + = “New”;

char s2* + = “Delhi”;

strcat (s1, s2);

printf(“%s”, s1);

The output will be NewDelhi


What is recursion? What are its different types?

What is Recursion?

Recursion in programming is a process where a function calls itself in order to solve


a problem. It is often used for problems that can be broken down into smaller, similar
subproblems. Recursion involves two key components:

1. Base Case: The condition under which the recursion stops.

2. Recursive Case: The part where the function calls itself with a modified argument.

In essence, recursion allows a problem to be solved by solving smaller instances of


the same problem.

Types of Recursion

There are two primary types of recursion:

1. Direct Recursion (also known as Simple Recursion)

Definition: Direct recursion occurs when a function calls itself directly within its own
body.

Example: A common example of direct recursion is calculating the factorial of a


number.

Example of Direct Recursion:

#include <stdio.h>

int factorial(int n) {

if (n == 0) { // Base case

return 1;

return n * factorial(n - 1); // Recursive case (function calls itself)

int main() {
int num = 5;

printf("Factorial of %d is %d\n", num, factorial(num));

return 0;

Explanation:

The factorial() function calls itself with n-1 until n reaches 0. This is direct recursion
because the function calls itself directly.

2. Indirect Recursion (also known as Mutual Recursion)

Definition: Indirect recursion occurs when a function calls another function, and that
second function, in turn, calls the first function. This creates a cycle of function calls.

Example: A typical example is two functions where one calls the other, and the second
calls the first.

Example of Indirect Recursion:

#include <stdio.h>

void funcA(int n); // Forward declaration

void funcB(int n); // Forward declaration

void funcA(int n) {

if (n > 0) {

printf("%d ", n);

funcB(n - 1); // Indirect recursion (calls funcB)

void funcB(int n) {

if (n > 0) {

printf("%d ", n);


funcA(n - 1); // Indirect recursion (calls funcA)

int main() {

int num = 5;

funcA(num); // Start the recursion chain with funcA

return 0;

Explanation:

The function funcA() calls funcB(), and funcB() calls funcA(). This mutual calling creates
indirect recursion.
Differentiate between call by value and call by reference with the help of an example.

Call by Value and Call by Reference are two methods of passing arguments to
functions in programming. Here's a breakdown of both concepts along with an
example:

1. Call by Value:

In call by value, the actual value of the argument is passed to the function. The
function works on a copy of the argument, and any modifications made to the
parameter inside the function do not affect the original argument.

Example of Call by Value:

def modify_value(x):

x = x + 10

print("Inside function:", x)

a=5

modify_value(a)

print("Outside function:", a)

Output:

Inside function: 15

Outside function: 5

Explanation:

The value of a (which is 5) is passed to the function modify_value.

Inside the function, x is modified (incremented by 10), but this does not change the
original value of a outside the function.

2. Call by Reference:

In call by reference, the reference (or address) of the argument is passed to the
function. This means that any modifications made to the parameter inside the function
will affect the original argument, as both the parameter and the argument point to
the same memory location.

Example of Call by Reference:

def modify_reference(lst):

lst.append(4)

print("Inside function:", lst)

my_list = [1, 2, 3]

modify_reference(my_list)

print("Outside function:", my_list)

Output:

Inside function: [1, 2, 3, 4]

Outside function: [1, 2, 3, 4]

Explanation:

The reference (memory address) of my_list is passed to the function


modify_reference.

Inside the function, an element is added to the list, and this modification reflects in the
original list outside the function as well, because both the parameter lst and the
argument my_list refer to the same object in memory.

You might also like