2. • To learn the features of C
• To learn the linear and non-linear data structures
• To explore the applications of linear and non-linear data
structures
• To learn to represent data using graph data structure
• To learn the basic sorting and searching algorithms
2
Course Objective
3. Upon completion of the course, students will be able to
• Implement linear and non-linear data structure operations using C
• Suggest appropriate linear / non-linear data structure for any given
data set.
• Apply hashing concepts for a given problem
• Modify or suggest new data structure for an application
• Appropriately choose the sorting algorithm for an application
3
Course Outcome
4. Pradip Dey and Manas Ghosh,
―Programming in C, Second
Edition, Oxford University
Press, 2011.
4
Text Book
5. Ellis Horowitz, Sartaj Sahni,
Susan Anderson-Freed, -
Fundamentals of Data
Structures in C, Second Edition,
University Press, 2008.
5
Text Book
6. 1. Mark Allen Weiss, ―Data Structures and Algorithm Analysis
in C, Second Edition, Pearson Education, 1996.
2. Alfred V. Aho, John E. Hopcroft and Jeffrey D. Ullman,
―Data Structures and Algorithms, Pearson Education, 1983.
Reference Books
7. 3. Robert Kruse, C.L.Tondo, Bruce Leung, Shashi Mogalla , ―
Data Structures and Program Design in C, Second Edition,
Pearson Education, 2007.
4. Jean-Paul Tremblay and Paul G. Sorenson, ―An Introduction
to Data Structures with Applications, Second Edition, Tata
McGraw-Hill, 1991.
Reference Books
8. Course Assessment
S. No Name of the
Assessment
Nos. Max. Marks Weightage %
1 CIA I 1 100 20 Marks
Note:
(Sum of CIA I + CIA II + CIA
III)
The total 300 Marks will be
converted into 20 Marks
2 CIA II 1 100
3 CIA III 1 100
4 University Exam 1 100 This will be converted into 80
Marks
Internal: 20 Marks Total: 100 Marks
External: 80 Marks
Note: You need to maintain minimum 80% attendance. If
your attendance is less than 80% then you won't be allowed to write your
final exams .
11. • What is a Program?
– The collection of detailed expressions/instructions
that supplied to a computer to perform a specific
task is known as a program.
– Example: Word Processors, Apps, Games, etc.,
• A computer program tells your computer how to do
what you want.
• C is a Programming language designed to create
System Application to interact with the hardware. 11
WHAT IS C PROGRAM?
12. • Objective of developing C Programming
Language:
– Developing an operating system used on multiple
computers.
• C is a lower level than most other languages
– i.e. it creates code that’s a lot closer to what
machines really understand.
• Procedural and Structured programming
Language
12
Why C?
14. • Simple & Structured Programming Language
– provides structured approach (to break the problem
into parts), rich set of library functions, data types
etc.
• Portable
– C programs can be executed on many machines
with a little bit or no change.
• Mid - Level Programming Language
– C is also used to do low-level programming. It is
used to develop system applications such as kernel,
driver etc. It also supports the feature of high level
language. 14
Features of C?
15. • Rich Library
– C provides a lot of inbuilt functions that make the
development fast
• Memory Management
– It supports the feature of dynamic memory
allocation.
• Speed
– The compilation and execution time of C language
is fast.
15
Features of C?
16. • Pointers
– It allows to interact with the memory directly by
using the pointers.
• Recursion
– Can call the function within the function.
– It provides code reusability for every function.
• Extensible
– It can easily adopt new features.
16
Features of C?
17. 17
Basic Structure of a C Program
Documentation Section
Link Section
Definition Section
Global Declaration Section
Main Function Section
Declaration Part
Executable Part
Sub- Program Section
User Defined Function
18. • Documentation Section
– It is a comment block with information which
helps in tracking and identifying the program.
• Link Section
– It provides instructions to the compiler to link
functions from the system library such as #include
directive.
• Definition Section
– It defines all symbolic constants such as using the
#define directive.
18
Basic Structure of C Program
19. • Global Declaration Section
– There are some variables that can be used in more
than one function.
– Such variables are called Global Variables and are
declared in the global declaration section.
• Main Function Section
– Every C Program must have one main function
section.
– Two Parts: Declaration Part & Executable Part
• Declaration Part
– Declares all the variables used in executable part
19
Basic Structure of C Program
20. • Executable Part
– At least one executable part.
– Appear between the opening and closing braces.
– The closing brace is the logical end of the
program.
– All statements ends with a semicolon.
• Sub - Program Section
– The sub program contains all the user defined
functions that are called in the main function.
20
Basic Structure of C Program
21. • The execution of C Programming language
involves a number of tasks/phases.
• To successfully develop an
application/program we need to have a pre-
processor, a compiler, a assembler and a
linker.
• C is a compiled programming language that
means, the code need to convert the source
files (human readable text files) into object
files (which can be understood by the machine:
microcontroller, computer/laptop).
21
How C Programming Language works?
28. • While solving a problem using a computer
program, all the data we want to work should
be stored in computer’s memory and if some
operation is performed, the date need to be
updated in memory.
• In order to store the data in memory, there are
certain ways to store, access and modify data
in computer’s memory.
28
CONSTANTS AND VARIABLES
29. • interest =
𝑝∗𝑛∗𝑟
100
– Here p, n and r Variable Values can Change
– 100 Constant Values will not change
– Interest Variable Name
• Constants are the values of different types that
can write directly in a program.
• Ex: An integer, real number or a sequence of
charactes
• 1000, 3.14, ‘a’, “Hello”
29
CONSTANTS AND VARIABLES
30. • Variables are unique name or identifier for a
section of computer’s memory where we can
fill the data.
• Ex: P,Q
30
CONSTANTS AND VARIABLES
P
Q
Constants and variables
means to store and represent
data.
Data can be of various types.
31. • Data type defines a set of values that a
variable can store along with a set of
operations that can be performed on it.
31
DATA TYPES
Data Types
Derived
Basic Enumeration Void
32. • Data type defines a set of values that a
variable can store along with a set of
operations that can be performed on it.
32
DATA TYPES
Data Types
Derived
Basic Enumeration Void
33. 33
DATA TYPES
Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
34. • To use variables along with data type, a
variable need to be declared first.
• Syntax datatype variable_name;
• Example:
#include<stdio.h>
int main()
{
int a; //Declaration
a=10; //Initialization
printf(“a=%d”,a);
} 34
DATA TYPES
35. Type Size (Bytes) Format Specifier Range
int 2 %d -32768 to 32767
char 1 %c -128 to 127
float 4 %f 3.4E-38 to 3.4E+38
double 8 %lf 1.7E-308 to 1.7E+308
short int 2 %hd -128 to 127
unsigned int 2 %u 0 to 65535
long int 4 %ld
-2147483648 to
2147483647
signed char 1 %c -128 to 127
unsigned char 1 %c 0 to 255
long double 10 %LF
3.4E-4932 to
35
DATA TYPES
36. 36
OPERATORS IN C
• An operator is a symbol that operates on a value or a
variable.
• These values are known as operands.
• For example: + is an operator to perform addition.
• C has a wide range of operators to perform various
operations.
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Bitwise Operators
• Misc Operators
37. 37
OPERATORS
• Arithmetic Operators:
• An arithmetic operator performs mathematical
operations such as addition, subtraction, multiplication,
division etc on numerical values.
–+, -, *, /, %, ++, --
• Assignment Operators:
• An assignment operator is used for assigning a
value to a variable.
– =, +=, -=, *=, /=, %=
38. 38
OPERATORS
• Relational Operators:
• A relational operator checks the relationship between
two operands. If the relation is true, it returns 1; if the
relation is false, it returns value 0.
==, >, <, !=, >=, <=
Relational operators are used in decision making and
loops.
• Assignment Operators:
• An assignment operator is used for assigning a
value to a variable.
– =, +=, -=, *=, /=, %=
40. 40
DECISION MAKING AND BRANCHING
• Instructions of a programs are executed either
– Sequential Manner
– Branching
• “Decision making and branching” is one of the most
important concepts of computer programming.
• Programs should be able to make logical (true/false)
decisions based on the condition provided.
• Controlling the execution of statements based on certain
condition or decision is called decision making and
branching.
41. 41
Condition Satisfied {True}
Condition not Satisfied {False}
Branching
Branch 1
Branch 2
START
Read User Input
Condition ?
Perform Some Tasks
Perform Some Other
Tasks
Show Result
END
42. 42
HOLIDAY TRIP PROBLEM
• Consider the fact that you and some of your friends have
planed to go out for a holiday trip after the Spring
Semester, 2014.
• You have also decided that if you have got received
money 10000 dollars or more from your parent then
your will go out for a foreign trip. Otherwise, if the
allotted money is less than 10000 then you will go out
for a country side trip.
• Now you are supposed to design a program to solve this
problem.
44. 44
DECISION MAKING AND BRANCHING IN C
• C language supports the following decision making
statements.
– if statement
– switch statement
– conditional operator
– goto statement
• These statements are knows as decision making
statements.
• They are also called control statements as the control
the flow of execution.
45. 45
IF STATEMENT
• The if statement is a powerful statement for decision
making and is used to control the flow of execution of
statements.
• It takes the following structure:
– if (test-condition)
• It allows the computer to evaluate the expression first
and then depending on whether the value of the
expression or condition is true or false, it transfer the
control to a particular statement.
47. 47
IF STATEMENT
• The if statement can be implemented if four different
forms depending on the complexity of the conditions to
be tested.
• The four forms are:
– Simple if statement
– If else statement
– Nested if else statement
– Else if ladder
48. 48
SIMPLE IF STATEMENT- STRUCTURE
• The general form of a simple if statement is:
if (test_condition)
{
statement-block;
}
statement x;
• When the condition is true then both the statement block
and the statement-x are executed but in sequence.
49. 49
SIMPLE IF STATEMENT- FLOWCHART
Entry
Test Condition?
Statement
Block;
Statement x;
Next Statement;
True
False
50. ▪ Important things to notice that if you want to control a
single statement using the if condition then no need to
create a block using the curly bracy.
▪ The curly bracy is used to create statement block when it is
necessary to include multiple statements under the same if
condition.
SIMPLE IF STATEMENT
51. Simple if Statement-Example
▪ Example with block of statement:
if (marks>=90)
{
marks=marks+ bonus_marks;
grade=“A+”;
}
printf(“The mark achieved:marks” , %d);
Condition controlled
Statement
52. Simple if Statement
▪Example of single statement:
if (marks>=90)
marks=marks+ bonus_marks;
printf(“The mark achieved:marks” , %d);
Condition controlled
statement
53. Simple if Statement
▪ What happens if multiple statements follows
after the if condition without using the curly
brace?
▪ Answer: Then the statement immediately following the if
condition is considered as the controlling statement of the
if condition. if the condition is true the controlled
statement is executed otherwise not. Rest of the
statements are executed sequentially.
54. if else Statement
▪ If the test condition is true then the true block statements,
immediately following the if statements are executed;
▪ Otherwise the false block statements are executed.
▪ In short either true-block or false-block of statements will be
executed, not both.
▪ But in both cases the control is transferred subsequently to the
statement-x as it is an independent (not controlled by the if
else statement) statement.
▪ It is also called two way conditional branching
55. if else Statement- Structure
▪ The if else statement is an extension of the simple if
statement. The general form is :
if (test_condition)
{
True block statements;
}
else
{
False block statements;
}
statement-x;
57. if else Statement- Example
▪Example with block of statement:
if (marks>=40)
{
marks=marks+ bonus_marks;
grade=“passed”;
}
else
{
marks=marks;
grade=“failed”;
}
printf(“The mark achieved:marks” , %d);
True block statement
False block statement
58. Nested if else Statement
▪ Using “if…else statement” within another “if…else
statement” is called ‘nested if statement’.
▪ “Nested if statements” is mainly used to test multiple
conditions.
▪ It is called nested conditional branching.
59. 59
NESTED IF ELSE STATEMENT- STRUCTURE
• The general form of a nested if
else statement is:
if (test_condition)
{
if(test_condition)
{
statement-
block;
}
else
{
statement block;
}
}
else
{
statement
block;
}
60. Nested if else -Example
if (gender==female)
{
if (age<10)
{
provide free entry;
provide free food;
}
else
{
provide only free entry;
}
}
else
{
statement-block;
}
Nested if else
61. Else if ladder Statement
▪ The word ladder means the staircase. As the name implies
this statement is used to choose right way/paths among
multiple paths.
▪ There is another way of putting if conditions together when
multiway decisions are involved.
▪ A multiway decision is a chain of if conditions in which the
statement associated with an else condition behaves like
another if condition.
▪ Else if ladder is also called 3 way or multiway decision
making statement.
62. Else if ladder- Structure
if (test_condition 1)
statement-1;
else if (test_condition 2)
statement-2;
else if (test_condition 3)
statement-3;
else if (test_condition 4)
statement-4;
…………………………
……….. else if
(test_condition n)
statement-n;
statement-x;
63. Else if ladder- Example
if(Mark>=50 && Mark<60)
{ printf("Your grade is D");
}
else if(Mark>=60 && Mark<70)
{ printf("Your grade is C n");
}
else if(Mark>=70 && Mark<80)
{ printf("Your grade is B n");
}
else if(Mark>=80 && Mark<90)
{ printf("Your grade is A n");
}
else
printf("you have failed");
64. Switch Statement
▪ When one of the many statements is to be selected, then if
conditional statement can be used to control the selection.
▪ However the complexity of such a program increases
dramatically when the number of statements increases.
▪ Fortunately, C has a built in multiway decision making
statement known as switch.
▪ The switch statement tests the value of a given variable or
expression against a list of case values and when a match is
found only then a block of statements associated with that case
is executed.
▪ The general form is given below:
65. Switch Statement-Structure
switch(expression/ value)
{
case value-1:
statement-block-1;
break;
case value-2:
statement-block-2;
break;
…………………
……. case
value-n:
statement-block-n;
break;
default:
default-statement-block;
break;
}
statement-x;
66. Rules for Switch Statement
▪ The switch statement must be an integral type.
▪ Case labels must be constant or constant expression.
▪ Case labels must be unique. No two labels can have the same value.
▪ Case labels must end with colon.
▪ The break statement transfer the control out of the switch
statement.
▪ The break statement is optional. So two or more case labels may
belong to the same statements.
▪ The default label is optional. If present, it will be executed when
the expression does not find a matching case label.
▪ There can be at most one default label.
▪ The default may be placed any where but usually placed at the
end.
▪ It is permitted to nest switch statements.
67. Conditional Operator
▪ The C language has an unusual operator which is useful for
making two way decisions.
▪ This operator is a combination of ? and :
▪ It takes three operands. This operator is popularly known as
the conditional operator.
▪ The conditional operator can be used as the replacement of if
else conditional statement for two way decision making.
68. Conditional Operator
▪ The general structure of conditional operator:
Conditional expression? true-statement 1: false-statement;
▪ The condition is evaluated first. If the result is true then the
statement 1 is executed and its value is returned.
▪ Otherwise statement 2 is executed and its value is
returned.
▪ Example:
flag = (x<0) ? 0 :1;
70. 70
GOTO STATEMENTS
• The goto statement allows us to transfer control of the
program to the specified label.
• Syntax of goto Statement:
goto label;
... .. ... ... .. ...
label:
statement;
71. 71
LOOPING STATEMENTS
In a program, want to print HAI more than Once!!!!!
HAI
HAI
HAI
HAI
HAI
HAI
HAI
HAI
HAI
HAI
Printf(“HAI”);
Printf(“HAI”);
Printf(“HAI”);
Printf(“HAI”);
Printf(“HAI”);
Printf(“HAI”);
Printf(“HAI”);
Printf(“HAI”);
Printf(“HAI”);
Printf(“HAI”);
No good
programmer
does this!!!!
72. 72
LOOPS
• Do not need to write the code for 10 times.
• A loop is a piece of code which allows one to execute a
statement or block of statements repeatedly.
• A loop can either be a pre-test loop or be a post-test
loop.
– Pre Test Loop: Entry Controlled Loop
– Post Test Loop: Exit Controlled Loop
Entry Controlled Loop Exit Controlled Loop
Checks the condition first and
then runs the loop body
Executes loop body first and
then checks for a condition
74. 74
WHILE LOOP
• In while loop it checks the condition first, if condition is
true then control goes inside the loop body other wise
goes outside the body.
• Syntax:
while (condition)
{
loop body;
increment or decrement;
}
Start
Condition
Loop Body
Loop Update
Process Results
Stop
If condition is
false
If condition is true
75. 75
WHILE STATEMENTS
• Example
int a=1;
while (a<4)
{
printf(“Hai”);
a++;
}
True
True
2 Hai
3
4
1
a
Hai
Hai
Output
Prints Hai
Increment by 1
True
Prints Hai
Increment by 1
True
Prints Hai
Increment by 1
False
Loop Exists
76. 76
ARRAYS
• An Array is a collection of similar data type value in a
single variable.
• It is a derived data type in C, which is constructed from
fundamental data type of C language.
0 1 2 3 4 5 6 7 8 9
Length of Array 10
First Index Element
(at 5th Index)
Indexes
77. 77
FEATURES OF AN ARRAY
• Arrays have 0 (Zero) as the first index, not 1 (One). In
this example, marks[0] is the first element.
• If the size of an array is N, to access the last element,
the N-1 index is used. In this example, mark[4].
• Suppose the starting address of mark[0] is 2120d. Then,
the address of the mark[1] will be 2124d. Similarly, the
address of marks[2] will be 2128d and so on.
marks[0] marks[1] marks[2] marks[3] marks[4]
78. 78
ARRAY - DECLARATION
• To declare an array in C, need to declare data type and
size of an Array or by initializing it or by both.
Declaration of an Array in C
123 765 1621
1 1 1
1 2 3
1 0 0
0 0 0
0 0 0
3 3
3 3 0
int *a;
int *a;
int *a;
int *a;
int a[3]; int a[3] = {1,2,3} int a[3] = {} int a[3] = {0…1} = 3;
int a[3] = {[0…1]=3}
int a[3] = {0}
int a[3] = {1,1,1}
int a[3] = {1}
79. 79
ARRAY DECLARATION
• Array declaration by specifying the array size:
datatype arrayName[SIZE];
• Examples:
// Array declaration by specifying size
int arr1[10];
// Declare an array of user specified size
int n = 10;
int arr2[n];
• Array declaration by initializing elements:
// Array declaration by initializing elements
int arr[] = { 10, 20, 30, 40 }
// Here Compiler creates an array of size 4.
80. 80
ARRAY DECLARATION
• Array declaration by specifying size and initializing
elements:
• Examples:
// Array declaration by specifying size and initializing
int arr[6] = { 10, 20, 30, 40 }
• In above syntax Compiler creates an array of size 6,
initializes first 4 elements as specified by user and rest
two elements as 0. above is same as "int arr[] = {10, 20,
30, 40, 0, 0}"
81. 81
ARRAY INITIALIZATION
• Initializing is a process to initialize the value in array
variable.
• Initialization of array one by one:
int arr[5];
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;
arr[4]=50;
• Initialization of array at once:
int arr[]={10,20,30,40,50};
82. 82
ACCESSING ARRAY ELEMENTS IN C
• Array elements can be accessed with the help of index
value of an element.
• Array index starts with 0 and goes till size of array -1.
• int arr[]={10,20,30,40,50};
arr[3] // here 3 is index value and it return 40
0 1 3 4 5 6
arr
arr[0];
Array Variable
Index of elements
to be accessed
84. 84
2 DIMENSIONAL ARRAY
• An array of arrays is known as 2D array.
• In 2-dimentional elements are arranged in row and
column format.
• When we are working with 2-dimentional array we
require to refer 2-subscript operator which indicates row
and column sizes.
data-type arrayName [SIZE] [SIZE];
85. 85
2 DIMENSIONAL ARRAY - INITIALIZATION
• There are two ways to initialize a two Dimensional
arrays during declaration.
• int a[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };
or
• int a[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
int abc[2][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second dimension*/
int abc[2][] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second dimension*/
86. 86
2 DIMENSIONAL ARRAY
• int a[2][4];
2 Dimensional Array Conceptual Memory Representation
• Here the array is a[2][4], which can be conceptually viewed as a matrix of 2 rows and 4
columns.
• Actual representation in memory as follows:
• Array is on Integer type, so each element would use 4 bytes.
a[0][0] a[0][1] a[0][2] a[0][3]
a[1][0] a[1][1] a[1][2] a[1][3]
a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3]
82206 82210 82214 82218 82222 82226 82230 82234
87. 87
2 DIMENSIONAL ARRAY - INITIALIZATION
#include<stdio.h>
int main(){
int disp[2][3];
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
} }
printf("Two Dimensional array elements:n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("n");
} } }
return 0;
}
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
1 2 3
4 5 6
88. 88
STRINGS
• String is a collection of character or group of
character, in a linear sequence which are represented
using double quotes.
• The string in C language is one-dimensional array of
character which is terminated by a null character '0'.
• Examples:
– “Welcome to the world of programming in C”
– “ Hello ECE”
• ‘C’ provides standard library <string.h> that contains
many functions which can be used to perform
complicated operations easily on Strings in C.
89. 89
DECLARATION
• Strings are declared in C in similar manner as arrays.
• Syntax:
– datatype stringname[SIZE];
• Example:
– char s[5];
90. 90
INITIALIZATION
• Strings can be initialized in different ways as follows:
char str[]="abcd"; ///* string size = ‘a'+‘b'+‘c'+‘d'+"NULL" = 5
*/
or
char str[5]="abcd";
or
char str[5]={'a','b','c','d','0'};
or
char str[]={'a','b','c','d','0'};
or
char str[5]={'a','b','c','d','0'};
or
char *c="abcd";
93. 93
IMPORTANT POINTS TO NOTE
• In declaration of string size must be required to mention
otherwise it gives an error.
char str[]; // Invalid
char str[10]; // Valid
• In declaration of the string size must be unsigned integer
value (not -ve or zero value) which is greater than zero
only.
char str[]; // Invalid
char str[0]; // Invalid
char str[-1]; // Invalid
char str[10]; // Valid
94. 94
IMPORTANT POINTS TO NOTE
• In Initialization of the string if the specific number of
character are not initialized it then rest of all character
will be initialized with NULL.
char str[5]={'5','+','A'};
str[0]; ---> 5
str[1]; ---> +
str[2]; ---> A
str[3]; ---> NULL
str[4]; ---> NULL
• In initialization of the string we can not initialized more
than size of string elements.
char str[2]={'5','+','A','B'}; // Invalid
95. 95
IMPORTANT POINTS TO NOTE
• In initialization of the string the size is optional in this
case how many variable elements are initialized it, that
array element will created.
char str[]={'5','+','A','B'}; // Valid
sizeof(str) --> 4byte
• When we are working with character array explicitly
NULL character does not occupies any physical memory
at the end of the character array.
char str[]={'h','e','l','l','o'}; //sizeof(str) --> 5byte
• String data at the end of the string NULL character
occupies physical memory.
char str[]="hello"; //sizeof(str) --> 6 byte
96. 96
STRING FUNCTIONS
• strlen:
– finds the length of a string
– strlen(string);
• strcpy:
– copies one string to another
– strcpy(destination,source)
• strcat:
– concatenates one string at the end of another
– strcat(string_one,string_two);
• strcmp:
– compares two strings lexicographically
– strcmp(string_one,string_two);
97. 97
STRING LENGTH(WITHOUT USING FUNCTION)
#include <stdio.h>
#include <conio.h>
void main()
{
char input_string[50];
int i=0, length=0;
clrscr();
printf("nEnter your text:t");
gets(input_string);
while(input_string[i]!='0')
{
length++;
i++;
}
printf("nThe length of your text is: %d character(s)", length);
getch();
}
Enter your text:
Enter your text: ECE
The length of your text is: 3
character(s)
98. 98
STRING LENGTH (USING STRLEN FUNCTION)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char input_string[50];
int length;
clrscr();
printf("nEnter your text:t");
gets(input_string);
length=strlen(input_string);
printf("nThe length of your text is: %d character(s)", length);
getch();
}
Enter your text: ECE
The length of your text is: 3
character(s)
99. 99
STRING COPY (WITHOUT USING FUNCTIONS)
#include <stdio.h>
#include <conio.h>
void main()
{
char copy[50], paste[50];
int i;
clrscr();
printf("nEnter your name (to copy):t");
gets(copy);
for(i=0;copy[i]!='0';i++)
{
paste[i]=copy[i];
}
paste[i]='0';
printf("nThe name is (pasted as):t");
puts(paste);
getch();
}
Enter your name: DEV
The name is (pasted as): DEV
100. 100
STRING COPY (USING STRCPY FUNCTION)
#include<stdio.h>
#include<conio.h>
void main()
{
char copy[50], paste[50];
int i;
clrscr();
printf("nEnter your name (to copy):t");
gets(copy);
strcpy(paste, copy);
printf("nThe name is (pasted as):t");
puts(paste);
getch();
Enter your name: DEV
The name is (pasted as): DEV
103. 103
STRING COMPARISION (STRCMP)
#include <string.h>
int main(){
char str1[20],str2[20];
printf("Enter 1st string: ");
gets(str1);//reads string from console
printf("Enter 2nd string: ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}
Enter 1st string:
Enter 1st string: Hai
Enter 2nd string:
Enter 2nd string: Hai
Strings are Equal