Академический Документы
Профессиональный Документы
Культура Документы
Strings
A string in C is a sequence of zero or more characters followed by a NULL '\0' character.
All the string handling functions are prototyped in: string.h or stdio.h standard header file.
For example:
"c string tutorial"
Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the
end of string.
Declaration of strings
Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type.
char s[5];
Initialization of strings
In C, string can be initialized in different number of ways.
char c[]="abcd"; OR,
char c[5]="abcd"; OR,
char c[]={'a','b','c','d','\0'}; OR;
char c[5]={'a','b','c','d','\0'};
2
Program1
#include <stdio.h>
void main()
{
char name[20];
printf("Enter name: ");
scanf("%s",name); //Read the string
printf("Your name is %s",name); //Display the string
}
Output
Enter name: Dennis Ritchie
Your name is Dennis Ritchie
Program2
#include <stdio.h>
void main()
{
char name[20];
printf("Enter name: ");
gets(name); //Read the string
printf("Your name is “);
puts(name); //Display the string
}
Output
Enter name: Dennis Ritchie
Your name is Dennis Ritchie
(i) strlen(s1);
Returns the length of string s1.
Program
#include<stdio.h>
#include<string.h>
void main()
{
3
char a[20]=”hello”;
printf(“\n Length of the string is :%d”,strlen(a));
}
Output
Length of the string is 5
(ii) strcpy(s1,s2);
Copying the string from s2 to s1.
Program
#include<stdio.h>
#include<string.h>
void main()
{
char a[20]=”hello”,b[20];
strcpy(b,a);
printf(“\n String a is %s & String b is %s, a, b);
}
Output
String a is hello & String b is hello
(iii) strcat(s1,s2);
This will concatnate s1 & s2 and assign it to s1.
Program
#include<stdio.h>
#include<string.h>
void main()
{
char a[20]=”hello”,b[20]=”welcome”;
strcat(a,b);
printf(“\n String a is %s & String b is %s, a, b);
}
Output
String a is hellowelcome & String b is welcome
(iv)strcmp(s1,s2);
This will compare the two string s1 & s2. If s1 is equal to s2 then it will return 0. Otherwise it
will return non zero.
Program
#include<stdio.h>
#include<string.h>
void main()
{
char a[20]=”hello”,b[20]=”welcome”;
if(strcmp(a,b)==0)
printf(“\n Strings are equal”);
else
printf(“\n Strings are not equal”);
}
4
Output
Strings are not equal
(v) strupr(s1);
It displays the string s1 in upper case.
Program
#include<stdio.h>
#include<string.h>
void main()
{
char a[20]=”hello”;
printf(“%s”,strupr(a));
}
Output
HELLO
(vi) strlwr(s1);
It displays the string s1 in lower case.
Program
#include<stdio.h>
#include<string.h>
void main()
{
char a[20]=”WELCOME”;
printf(“%s”,strlwr(a));
}
Output
Welcome
(vii) strrev(s1);
It displays the string s1 in reverse order.
Program
#include<stdio.h>
#include<string.h>
void main()
{
char a[20]=”welcome”;
printf(“%s”,strrev(a));
}
Output
emoclew
Enter n strings:
Balu
Vinoth
Priya
Sekar
Raju
Searching
Searching is a method of finding the element or items is present in the given set or list. It can be
applied in many real life situations. For example, search a word in the dictionary, search a phone
number in the telephone directory, search the address in the street and so on.
6
Types of Searching
There are two types of searching methods are there:
Linear search
Binary search
Algorithm
Step 1: Get the search item X and the input array A with size n.
Step 2: Compare the search item X and compare with the first item i.e., a[0]. If it is matched, then
return the corresponding position of the array element and exit from the program.
Step 3: Otherwise, increment the array index by 1 and compare the array element with X.
Step 4: Repeat the process, until we find the solution.
Step 5: If the element is not found in the given array, then display the eroro ar “ Element is not found”
7
}
printf("Enter the number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);
getch();
}
Output
Enter the number of elements in array
5
Enter 5 elements
5
6
3
2
7
Enter the number to search
2
2 is present at location 4
8
Algorithm
Algorithm is quite simple. It can be done either recursively or iteratively:
1. get the middle element;
2. if the middle element equals to the searched value, the algorithm stops;
3. otherwise, two cases are possible:
o Searched value is less, than the middle element. In this case, go to the step 1 for the part of the
array, before middle element.
o Searched value is greater, than the middle element. In this case, go to the step 1 for the part of
the array, after middle element.
9
Linear Search vs. Binary Search
Unit – IV
Functions
Functions are smaller self-contained components which carry out some specific, well defined task. The
benefits are:
It facilitates top-down modular programming
It avoids the need for redundant code
It facilitates reusability
It can be used to build a customized library of frequently used routines
Syntax
return_type function_name (function arguments);
Example
int find_big(int,int);
Function find_big returns integer value, takes 2 integer arguments
void swap(int *,int *);
Function swap does not return any value, takes 2 pointer variables.
float add(float, int);
Function add returns float value, takes 1 float variable and 1 integer variable.
10
(ii) Function Definition
It is used to define the function with appropriate name, parameters, and the task to be carried out by
the function. It can be defined at any location in the program. A function definition has two principle
components: Function header (first line) and Function body.
Syntax
return-type fn-name(type arg1, type arg2, ……)
{
local variable declaration;
executable statement 1;
executable statement 2;
……………..
return expression;
}
A function definition in C programming language consists of a function header and a function body.
Here are all the parts of a function:
Return Type: A function may return a value. The return_type is the data type of the value the function
returns. Some functions perform the desired operations without returning a value. In this case, the
return_type is the keyword void.
Function Name: This is the actual name of the function. The function name and the parameter list
together constitute the function signature.
Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the
parameter. This value is referred to as actual parameter or argument. The parameter list refers to the
type, order, and number of the parameters of a function. Parameters are optional; that is, a function
may contain no parameters.
Function Body: The function body contains a collection of statements that define what the function
does.
Syntax
[vble name]= fn-name(actual arguments);
Recursion
A function which call by itself is called recursion. A recursive function must have the following
properties: The problem must be written in a recursive form. There must be a base criteria
(terminating condition) for which the function doesn’t call itself.
Output
Enter a number: 5
Factorial of 5 is: 120
12
Types of Function
Functions can be calssified into four categories based on arguments passed and the return type. They
are
Functions with no arguments and no return value
Functions with no arguments but return value
Functions with arguments but no return value
Functions with arguments and return value
Program
#include<stdio.h>
void add(); //Function Prototype
void main()
{
add(); //Function Call
}
void add() //Function Definition
{
int a,b,c;
printf(“\n Enter a & b:”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“\n Result is %d”,c);
}
Output
Enter a& b:
3
4
Result is 7
Program
#include<stdio.h>
int add(); //Function Prototype
void main()
{
int c;
13
c=add(); //Function Call
printf(“\n Result is %d”,c);
}
int add() //Function Definition
{
int a,b;
printf(“\n Enter a & b:”);
scanf(“%d%d”,&a,&b);
c=a+b;
return c;
}
Output
Enter a & b:
3
4
Result is 7
Program
#include<stdio.h>
void add(int,int); //Function Prototype
void main()
{
int a,b;
printf(“\n Enter a & b:”);
scanf(“%d%d”,&a,&b);
add(a,b); //Function Call
}
void add(int x,int y) //Function Definition
{
int c;
c=x+y;
printf(“\n Result is %d”,c);
}
Output
Enter a & b:
3
4
14
Result is 7
Program
#include<stdio.h>
int add(int,int); //Function Prototype
void main()
{
int a,b,c;
printf(“\n Enter a & b:”);
scanf(“%d%d”,&a,&b);
c=add(a,b); //Function Call
printf(“\n Result is %d”,c);
}
int add(int x,int y) //Function Definition
{
return (x+y);
}
Output
Enter a & b:
3
4
Result is 7
Parameter Passing
A function is referenced by using its name and providing appropriate values for the arguments. There
are two approaches to pass the information to a function via arguments. They are:
Call by Value
Call by Reference
Sample Program
#include <stdio.h>
void swap(int, int); // function declaration
void main ()
{
int a = 100, b=200;
printf("Before swap, value of a : %d\n", a );
15
printf("Before swap, value of b : %d\n", b );
swap(a, b); //Function Call
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
}
void swap(int x, int y) //function definition
{
int temp;
temp = x;
x = y;
y = temp;
}
Output
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200
Sample Program
#include <stdio.h>
void swap(int *, int *); // function declaration
void main ()
{
int a = 100, b=200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
swap(&a, &b); //Function Call
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
}
void swap(int *x, int *y) //function definition
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Output
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100
16
Actual parameter Vs formal parameter
17