0% found this document useful (0 votes)
23 views7 pages

Cunit III

ARRAYS AND STRINGS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

Cunit III

ARRAYS AND STRINGS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

1 Strings and Functions

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.

Declaration:-Refer to the declaration given below –

char stringname [size];

For example - char a[50]; a string of length 50 characters.

Initialization:-The initialization is as follows −

Using single character constant −

char string[20] = { ‘H’, ‘i’, ‘l’, ‘l’, ‘s’ ,‘\0’}

‘H’ ‘i’ ‘l’ ‘l’ ‘s’ ‘\0’

Using string constants −

char string[20] = "Hello":;

‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’

‘\0’ is called a null character. It marks the end of the string.


‘\0’ is automatically placed by the compiler, if a string is given as input. The user has to take care
of placing ‘\0’ at the end if a single character is given.
Example
Following is the C program for a string −
#include<stdio.h>
main ( ){
char a[10] = "Hello";
clrscr ( );
printf ( " given string is %s",a)
getch ( );
}
Output
When the above program is executed, it produces the following result −
Given string is Hello
2 Strings and Functions

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);

3. strcat( ):-strcat( ) is used to concatenate two strings.


The Destination_String should be a variable and Source_String can either be a string
constant or a variable.
Syntax: strcat(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);

5. Strupr():-The strupr( ) function is used to converts a given string to uppercase.


Syntax: char *strupr(char *str);

6 . Strlwr():-The strlwr( ) function is used to converts a given string to lowercase.


Syntax: char *strlwr(char *str);
3 Strings and Functions

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.

There are 3 aspects in each C function. They are,

 Function declaration or prototype:- - This informs compiler about the function name,
function parameters and return value’s data type.

 Function call:- This calls the actual function


 Function definition:- This contains all the statements to be executed.
Example:-
#include<stdio.h>
float square ( float x );
int main( )
{

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:-

Enter some number for finding square


2
Square of the given number 2.000000 is 4.000000
4 Strings and Functions

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.

Example program for C function (using call by value):-

#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);
}

void swap(int a, int b)


{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d", a, b);

Output:-

values before swap m = 22


and n = 44
values after swap m = 44
and n = 22
5 Strings and Functions

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.

Example program for C function (using call by reference):-

#include<stdio.h>
void swap(int *a, int *b);
int main()
{

int m = 22, n = 44;


printf("values before swap m = %d \n and n = %d",m,n);
swap(&m, &n);

void swap(int *a, int *b)


{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("\n values after swap a = %d \nand b = %d", *a, *b);

Output:-

values before swap m = 22

and n = 44
values after swap a = 44
and b = 22
6 Strings and Functions

5)Recursive Function:-Recursion is the process of repeating items in a self-similar way. In


programming languages, if a program allows you to call a function inside the same function, then it is
called a recursive call of the function.
void recursion( )
{
recursion( ); /* function calls itself */
}
int main( ) {
recursion( );
}
The C programming language supports recursion, i.e., a function to call itself. But while using recursion,
programmers need to be careful to define an exit condition from the function, otherwise it will go into an
infinite loop.

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

You might also like