0% found this document useful (0 votes)
54 views45 pages

Unit-Ii Fop

The document covers control structures in programming, focusing on decision-making, looping, and arrays. It explains various decision-making statements such as if, if-else, nested if-else, and switch-case, along with looping constructs like while, do-while, and for loops. Additionally, it discusses jump statements including break, continue, and goto, providing examples and syntax for each concept.

Uploaded by

nikhilnikhilgour
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)
54 views45 pages

Unit-Ii Fop

The document covers control structures in programming, focusing on decision-making, looping, and arrays. It explains various decision-making statements such as if, if-else, nested if-else, and switch-case, along with looping constructs like while, do-while, and for loops. Additionally, it discusses jump statements including break, continue, and goto, providing examples and syntax for each concept.

Uploaded by

nikhilnikhilgour
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/ 45

Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.

R ollege/Hosur/2024

UNIT-II
Decision Making , Looping and Arrays-Decision Making and Branching:
Introduction – if, if….else, nesting of if …else statementselse if ladder –
The switch statement, The ?: Operator – The go to Statement. Decision
Making and Looping: Introduction- The while statement- the do
statement – the for statement-jumps in loops. Arrays – Character Arrays
and Strings
Introduction
 Control structures are used to transfer control form one statement in
a program to any other statement
The control statements are classified as follows

Fundamentals of Computer Programming/2023 Regulations

Page 1
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Decision Making Statements

 Decision making is about deciding the order of execution of


statements based on certain conditions or repeat a group of
statements until certain specified conditions are met
Decision Making with if Statement

 The if statement may be implemented in different forms depending on


the complexity of conditions to be tested

Simple if Statement
 C uses the keyword if to execute a set of statements when logical
condition is true

Fundamentals of Computer Programming/2023 Regulations

Page 2
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

 When the logical condition is false, the compiler simply skips


the statement within the block
 The "if" statement is also known as one way decision statement

Syntax:
if(condition)
{
//Statement to be executed
//if condition is true
Statement 1;
Statement 2;
. .
. .

Statement n;
}
Flow diagram

 Here ‘Block of statement‘will wok if and only if the condition is true.


 ‘Statement 2‘ will work even the condition is true or false
Example
#include <stdio.h>
Fundamentals of Computer Programming/2023 Regulations

Page 3
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

int main()
{
int x = 15;
if (x > 20)
{
printf("15 is greater than 20");
}
printf("The statement executed is outside the if block");
}
Output
The statement executed is outside the if block
The if...else Statement

 C uses the keywords if and else to execute a set of statements


either logical condition is true or false
 If the condition is true, the statements under the keyword if
will be executed
 If the condition is false, the statements under the keyword else
will be executed

Syntax
if(condition)
{
//Execute this block
//if condition is true
}
else

Fundamentals of Computer Programming/2023 Regulations

Page 4
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

{
//Execute this block
//if condition is false
}
Flow diagram

Example Program
#include <stdio.h>
int main(void)
{
int n;
printf("\n Enter the number : ");
scanf("%d", &n);
if(n%2 == 0)
printf("\n %d is even", n);
else
printf("\n %d is odd", n);
return 0;
}

Fundamentals of Computer Programming/2023 Regulations

Page 5
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Output:
Enter the number : 4
4 is even
Nested if...else Statement

Syntax
if(Test Expression 1)
{
if(Test Expression 2)
{
statement block 1;
}
else
{
statement block 2;
}
}
else
{
if(Test Expression 3)
{
statement block 3;
}
else
{
statement block 4;
}
}

The nested if...else statement works as follows:


Fundamentals of Computer Programming/2023 Regulations

Page 6
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

 First, Test Expression 1 is checked, if it is true, then the Test Expression


2 is checked, if it is true then statement block 1 is executed. Otherwise,
statement block 2 is executed
 Otherwise, if the Test Expression 1 is false, then Test Expression 3 is
checked, if it is true then statement block 3 is executed. Otherwise,
statement block 4 is executed
Flow diagram

Fundamentals of Computer Programming/2023 Regulations

Page 7
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Example Program

#include <stdio.h>
int main(void)
{
int a, b, c;
printf("Enter three numbers : ");
scanf("%d%d%d \n",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}

Fundamentals of Computer Programming/2023 Regulations

Page 8
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

}
return 0;
}
Output
Enter three numbers : 12 9 11
a is the greatest
The if-else-if statement
 If else ladder is used, If we want to check for multiple conditions one
after another
Syntax
if(Test Expression 1)
{
statement block 1;
}
else if(Test Expression 2)
{
statement block 2;
}
...........................
else
{
statement block x;
}
statement y;

Fundamentals of Computer Programming/2023 Regulations

Page 9
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Flow Diagram

Example Program
#include <stdio.h>
int main(void)
{
int num;
printf("\n Enter any number : ");
scanf("%d", &num);
if(num==0)
printf("\n The value is equal to zero");
else if(num > 0)
printf("\n The number is positive");
else
printf("\n The number is negative");
Fundamentals of Computer Programming/2023 Regulations

Page 10
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

return 0;
}
Output
Enter any number : 12
The number is positive
Switch Case
 The switch case statement is used when we have multiple options and
we need to perform a different task for each option
 A switch statement is an alternative to if-else-if statement
The switch statement has four important elements.
 The test expression
 The case statements
 The break statements
 The default statement
Syntax
switch (expression)
{
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
.
.
.

Fundamentals of Computer Programming/2023 Regulations

Page 11
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

case valueN:
// statement sequence
break;
default:
// default statement sequence
}
Flow Diagram

Fundamentals of Computer Programming/2023 Regulations

Page 12
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Example Program
#include <stdio.h>
int main()
{
int i=2;
switch (i)
{
case 1:
printf("Case1 ");
break;
case 2:
printf("Case2 ");
break;
case 3:
printf("Case3 ");
break;
case 4:
printf("Case4 ");
break;
default:
printf("Default ");
}
return 0;
}
Output
Case 2

Fundamentals of Computer Programming/2023 Regulations

Page 13
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Looping Statements

while Loop

 The while loop in C is used to evaluate a test condition and iterate over
the loop body until the condition returns True

 The loop ends when the condition returns False

 This loop is also known as a pre-tested loop

 It is commonly used when the number of iterations is unknown to the


user ahead of time.

Syntax
while (condition)
{
// Code to execute as long as the condition is true
}
Here,
while: This is the keyword that begins the while loop
condition: This is a Boolean expression
The code inside the curly braces {} is the body of the loop
Flow diagram

Fundamentals of Computer Programming/2023 Regulations

Page 14
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Example
#include<stdio.h>
int main()
{
int number=10;

while(number <= 20)


{
printf("%d ", number);
number = number + 1;
}
return 0;
}
Output
10 11 12 13 14 15 16 17 18 19 20
do…while Loop in C
 The do…while in C is a loop statement used to repeat some part of the
code till the given condition is fulfilled
 It is a form of an exit-controlled or post-tested loop
 The test condition is checked after executing the body of the loop
 The statements in the do…while loop will always be executed at least
once
Syntax
do
{
// body of do-while loop

Fundamentals of Computer Programming/2023 Regulations

Page 15
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

} while (condition);
Flow diagram

Example
#include <stdio.h>
int main()
{
// loop variable declaration and initialization
int i = 0;
// do while loop
do {
printf("Skill Vertex\n");
i++;
} while (i < 3);
return 0;
}
Output
Skill Vertex
Skill Vertex
Skill Vertex

Fundamentals of Computer Programming/2023 Regulations

Page 16
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

For loop
 This is one of the most frequently used loop in C programming
 For loop is a loop in which all three statements initialization,
condition and increment/decrement is given in a single step (before
the code)
 Code may be executed for 0 or more times
 A loop is used for executing a block of statements repeatedly until a
given condition returns false
 It is good if number of iteration(repetition) is known by the user
Syntax
for ( starting value; condition; incrementation or
decrementation )
{
statements;
}

Fundamentals of Computer Programming/2023 Regulations

Page 17
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Flow diagram

Example
#includ<stdio.h>
int main()
{
int i;
for (i=1; i<=3; i++)
{
printf("%d\n", i);
}
return 0;
}
Output
1
2
3
Jump Statements

Fundamentals of Computer Programming/2023 Regulations

Page 18
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

 Jump statements in C are used to alter the normal sequence of


execution of a program
 They allow the program to transfer control to a different part of the
code
Types of Jump Statements in C
 break
 continue
 goto
 return
Break
 “break” is a keyword in C used to terminate a block of code that
controls the flow of the program such as switch-case and loops
Syntax

Flow diagram

Fundamentals of Computer Programming/2023 Regulations

Page 19
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Example
#include <stdio.h>
int main()
{
int num =0;
while(num<=100)
{
printf("value of variable num is: %d\n", num);
if (num==2)
{
break;
}
num++;
}
printf("Out of while-loop");
return 0;
}
Output
value of variable num is: 0
value of variable num is: 1
value of variable num is: 2
Out of while-loop
Continue
 The “continue” keyword is used to skip an iteration of the loop if a
certain condition is met
 It is used inside loop along with the decision-making statements

Fundamentals of Computer Programming/2023 Regulations

Page 20
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

 When continue statement encounters, the execution will go to the


starting of the loop
 The statements below continue statement will not be executed, if the
given condition becomes true
Syntax
continue;
Pictorial Explanation

Flow Diagram

Fundamentals of Computer Programming/2023 Regulations

Page 21
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Example
#include<stdio.h>

int main()
{
int i;

for(i=1;i<=100;i++)
{
if(i == 25 || i == 50 || i == 75)
continue;

printf("%d\n",i);
}

return 0;
}
Output
It will print numbers from 1 to 100 except the numbers 25,50 and 75

goto
 C programming introduces a goto statement by which you can jump
directly to a line of code within the same file
 The programmer needs to specify a label or identifier with the goto
statement

Fundamentals of Computer Programming/2023 Regulations

Page 22
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Syntax

Flow Diagram

Example
#include<stdio.h>
int main ()

Fundamentals of Computer Programming/2023 Regulations

Page 23
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

{ /* local variable definition */ int a = 10;


/* do loop execution */
LOOP:
do
{
if( a == 15) { /* skip the iteration */
a = a + 1;
goto LOOP;
}
printf("value of a: %d\n", a);
a++; }while( a < 20 );
return 0;
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Return
 The return statement terminates the execution of a function and
returns control to the calling function

Fundamentals of Computer Programming/2023 Regulations

Page 24
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

 Every function should have a return statement as its last statement


 The default return type is int
Syntax

Example

Fundamentals of Computer Programming/2023 Regulations

Page 25
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Arrays
 C Programming Arrays is collection of the Elements of the same data
type
 All Elements are stored in the Contiguous memory
 All elements in the array are accessed using the subscript variable
(index)
 C programming structure to store fixed-size, multiple values

Fundamentals of Computer Programming/2023 Regulations

Page 26
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

 It is used to hold a collection of primitive data, such as float, char,


int, etc

Array Terminologies
Size:
 Number of elements or capacity to store elements in an array
 It is always mentioned in square brackets [ ]
Type:
 Refers to data type
 It decides which type of element is stored in the array
Base:
 The address of the first element is a base address
 The array name itself stores address of the first element
Index:
 The array name is used to refer to the array element. For example
num[x], num is array and x is index
 The value of x begins from 0
 The index value is always an integer value

Fundamentals of Computer Programming/2023 Regulations

Page 27
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Range:
 Value of index of an array varies from lower bound to upper bound
For example in num[100] the range of index is 0 to 99
Word:
 It indicates the space required for an element
 In each memory location, computer can store a data piece
 The space occupation varies from machine to machine
 If the size of element is more than word (one byte) then it occupies
two successive memory locations
 The variables of data type int, float, long need more than one byte in
memory
One-dimensional Arrays
 The one-dimensional arrays in C language have just one dimension
 They are represented by a single row or column
Syntax
data_type array_name [size];
Array declaration

Fundamentals of Computer Programming/2023 Regulations

Page 28
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Example
#include<stdio.h>
int main() {
// Declare an array of integers
int numbers[5];
// Initialize the array elements
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Access and print array elements
printf("Array elements: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
Output
Array elements: 10 20 30 40 50
Initialize an Array
 Initializing an array means assigning an initial value to the variable
After array declaration
Syntax
data_type array_name [size] = {value1, value2, ... valueN};

Fundamentals of Computer Programming/2023 Regulations

Page 29
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Access Array Elements


 Access an element by indexing the array name
 The array subscript operator [ ] to place the index after the array
name
 Index will start from ‘0’ to ‘array_size-1”. This is known as zero-
based indexing
Syntax
Array_name [index];

Fundamentals of Computer Programming/2023 Regulations

Page 30
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Example
Initializing and Accessing Array Elements
#include<stdio.h>
int main() {
// Declare and initialize an array
int numbers[] = {10, 20, 30, 40, 50};
// Access and print array elements
printf("Array elements: ");
printf("%d ", numbers[0]); // Accessing the first element
printf("%d ", numbers[1]); // Accessing the second element
printf("%d ", numbers[2]); // Accessing the third element
printf("%d ", numbers[3]); // Accessing the fourth element
printf("%d ", numbers[4]); // Accessing the fifth element
printf("\n");
return 0;
}
Output
Array elements: 10 20 30 40 50
Example Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,t,n;
clrscr();
scanf("%d",&n);

Fundamentals of Computer Programming/2023 Regulations

Page 31
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
Input
8
5
3
6
1
Output
1

Fundamentals of Computer Programming/2023 Regulations

Page 32
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

3
5
6
8
Multi-dimensional Arrays in C
 They have more than one dimension, such as 2-D and 3-D arrays
 Two dimensional arrays will be in the form of rows and columns
format
Syntax of 2-D Array
datatype array_name[size1] [size2];
Example
#include<stdio.h>
int main()
{
int i, j;
int a[3][2] = { { 1, 4 },{ 5, 2 },{ 6, 5 }};
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}

Fundamentals of Computer Programming/2023 Regulations

Page 33
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Output
14
52
65
Example
#include<stdio.h>
int main()
{
int i, j;
int a[3][2] = { { 1 },{ 5, 2 },{ 6 }};
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
Output
10
52
60
Example
#include<stdio.h>

Fundamentals of Computer Programming/2023 Regulations

Page 34
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

int main()
{
int rowCount, columnCount, i, j;
int firstMatrix[10][10], secondMatrix[10][10],
resultMatrix[10][10];

printf("Number of rows of matrices to be added : ");


scanf("%d", &rowCount);

printf("Number of columns matrices to be added : ");


scanf("%d", &columnCount);

printf("Elements of first matrix : \n");

for (i = 0; i < rowCount; i++)


for (j = 0; j < columnCount; j++)
scanf("%d", &firstMatrix[i][j]);

printf("Elements of second matrix : \n");

for (i = 0; i < rowCount; i++)


for (j = 0; j < columnCount; j++)
scanf("%d", &secondMatrix[i][j]);

printf("Sum of entered matrices : \n");

Fundamentals of Computer Programming/2023 Regulations

Page 35
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

for (i = 0; i < rowCount; i++)


{
for (j = 0; j < columnCount; j++)
{
resultMatrix[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
printf("%d\t",resultMatrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
Number of rows of matrices to be added : 2
Number of columns matrices to be added : 2
Elements of first matrix :
5
5
5
5
Elements of second matrix :
1
1
1
1
Difference of entered matrices :
6 6

Fundamentals of Computer Programming/2023 Regulations

Page 36
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

6 6
Character Arrays
 Character arrays to store strings and then we can apply multiple
operations on strings like modifying a string, copying a string,
concatenation of a string to another, finding properties of strings like
length of the string etc.
 The character array should be large enough to accommodate the
string. i.e. If we have a string “Rahul” then to store the string we need
a character array of size 6
Syntax
storageclass chardatatype arrayname[exp];
Example
char name[50];
Initializing the Character Array
 Each element of the array is placed in a definite memory space and
each element can be accessed separately
 The array element should end with the null character as a reference
for the termination of the character array
 The computer will treat them as a single character
char name[5]="Rahul";
The elements would be assigned to each of the character array
position in the following way
name[0]='R'; name[1]='a'; name[2]='h'; name[3]='u'; name[4]='l';
Character Array in the Memory

Fundamentals of Computer Programming/2023 Regulations

Page 37
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

Example
#include <stdio.h>
int main() {
// Character array example
char charArray[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Character Array: %s\n", charArray);
Output
Character Array: Hello

Example
#include <stdio.h>
#include <conio.h>
#define NUM_ITEMS 5 //optional
main()
{
charstr[12];
str[0]='H'; str[1]='e'; str[2]='l'; str[3]='l'; str[4]='o';
str[5]=' '; str[6]='W'; str[7]='o'; str[8]='r'; str[9]='l';
str[10]='d'; str[11]='\0';

printf( "Your string is: %s", str );


getch();
}
Output
Your string is: Hello World

Fundamentals of Computer Programming/2023 Regulations

Page 38
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

The above building up the string by setting each character in


the array, so that the entire array looks something like the table below

str[number] 0 1 2 3 4 5 6 7 8 9 10 11
Contents H E l l o W o r l d \0
ASCII code 72 101 108 108 111 32 87 111 114 108 100 0

Strings
 Sequence of characters enclosed in double quotes is called a string
 A character array is also called as a string
 Every string ends with a null character '\0'
Example
“string_constant”
Declaring String
A string variable should be declared as a character type array and is
used to store string
Syntax
datatype variablename [size];
Example
char city[10];

String Handling Functions


 These are used to carry out string manipulations
 String handling functions are available in the header file string.h
The functions are given below
1.strlen()-Counting the length of a string
2.strcat()-Joining two strings
Fundamentals of Computer Programming/2023 Regulations

Page 39
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

3.strcmp() -Comparing two strings


4.strrev()-Reversing string
5.strupr()-Converting lower case string to upper case
6.strlwr()- Converting upper case string to lower case
7.strcpy()-Copying one string to another
Strlen()
 strlen() function is used to find out the length of the given string
 It takes the string as a parameter and returns the integer value

Syntax
strlen(str);
Example
strlen("computer");

Example
#include.stdio.h>
main()
{
char s[100];
static int len;
clrscr();
printf("\nEnter the string:");
gets(s);
len=strlen(s);
printf("\nlength=%d",len);
getch();
}
Output

Enter the string:computer

Fundamentals of Computer Programming/2023 Regulations

Page 40
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

length=8

strcat()

 strcat() function is used to combine two given strings. It takes two


string parameters
Syntax
strcat(str1,str2);
Example
strcat("computer","science");

Example
#include.stdio.h>
main()
{
char s[100],c[100];
clrscr();
printf("\nEnter first string:");
gets(s);
printf("\nEnter second string:");

Fundamentals of Computer Programming/2023 Regulations

Page 41
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

gets(c);
printf("\nAfter concatenation: %s",strcat(s,c));
getch();
}
Input
Enter first string:computer

Enter second string:science

Output
After concatenation: computerscience
strcmp()

 strcmp() function is used to compare two given strings


 It takes two string parameters
 The strcmp() returns 0 if two strings are alphabetically equal two
strings are alphabetically equal
 The strcmp() returns +ve value if first string is alphabetically greater
than the second string
 The strcmp() returns the -ve value if first string is alphabetically less
than the second string
Syntax
strcmp(str1,str2);
Example
strcmp("aeri","aeri");

Example
#include.stdio.h>
main()
{
char s[100],c[100];

Fundamentals of Computer Programming/2023 Regulations

Page 42
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

clrscr();
printf("\nEnter first string:");
gets(s);
printf("\nEnter second string:");
gets(c);
if(strcmp(s,c)==0)
printf("strings are equal");
else if(strcmp(s,c)>0)
printf("\n first string is biggerthan second");
else
printf("\nsecond string is biggerthan first");
getch();
}
Input
Enter first string:HOSUR
Enter second string:HOSUR
output
strings are equal
strrev()
 strrev() function is used to reverse the given string
 It takes the string as the parameter
Syntax
strrev(str);
Example
strrev("aeri");

Example
#include<stdio.h>
main()
{

Fundamentals of Computer Programming/2023 Regulations

Page 43
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

char s[100];
clrscr();
printf("\nEnter the string:");
gets(s);
printf("\nreverse string is=%s",strrev(s));
getch();
}
Input
Enter the string:aeri
Output
reverse string is=irea

strupr()
 strupr() function is used to convert the given string into uppercase
 It takes the string as parameter
Syntax
strupr(str);
Example
strupr("aeri");

Example
#include<stdio.h>
main()
{
char s[100];
clrscr();
printf("\nEnter string:");
gets(s);
printf("\nstring in uppercase:%s",strupr(s));
getch();

Fundamentals of Computer Programming/2023 Regulations

Page 44
Udayakumar C/Asst. Prof./B.Sc CS(AI&DS)/M.G.R ollege/Hosur/2024

}
Input
Enter string:aeri
Output

string in uppercase:AERI
strlwr()
 strlwr() function is used to convert the given string into lowrcase
 It takes the string as parameter

Syntax
strlwr(str);
Example
strlwr("aeri");

Example
#include<stdio.h>
main()
{
char s[100];
clrscr();
printf("/nEnter string:");
gets(s);
printf("/nstring in lowercase:%s",strlwr(s));
getch();
}
Input
Enter string:AERI
Output
string in lowercase:aeri

Fundamentals of Computer Programming/2023 Regulations

Page 45

You might also like