Module 2
Module 2
PROGRAMMING IN C
Prof. Sarju S
11 February 2025
Module 2
Page 2
Module 2
► Strings - Declaring a string variable, Reading and displaying strings, String related
library functions – Programs for string matching.
Page 3 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arrays
Page 4
Arrays
Page 5 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arrays
► Can you imagine how long we have to write the declaration part by using
normal variable declaration?
int main(void)
{
int studMark1, studMark2, studMark3, studMark4, …, …, studMark998,
stuMark999, studMark1000;
…
…
return 0;
}
Page 6 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arrays
► By using an array, we just declare like this,
► int studMark[1000];
► This will reserve 1000 contiguous memory locations for storing the students’ marks.
► Graphically, this can be depicted as in the following figure.
Index/subscript
starting from 0
0 43 studMar[0] Instead of 1
1 50 studMar[1]
Stored data of type integer
2 62 studMar[2]
3 33 studMar[3]
. . …..
. . …..
Arrays name
. . ….
Memory Storage 999 110 studMar[999]
Page 7 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arrays
Page 8 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arrays - One Dimensional Array: Declaration
► Dimension refers to the array's size, which is how big the array is.
► A single or one-dimensional array declaration has the following form,
datatype arrayName[arraySize];
► Here, datatype define the base type of the array, which is the type of each element
in the array.
► arrayName is any valid C / C++ identifier name that obeys the same rule for the
identifier naming.
► arraySize defines how many elements the array will hold.
Page 9 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arrays
Page 10 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arrays
Page 11 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arrays - Array Initialization
Page 12 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arrays - Array Initialization
► For example:
int idNum[7] = {1, 2, 3, 4, 5, 6, 7};
float fFloatNum[5] = {5.6, 5.7, 5.8, 5.9, 6.1};
char chVowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};
► The first line declares an integer array idNum and it immediately assigns the values 1,
2, 3, ..., 7 to idNum[0], idNum[1], idNum[2],..., idNum[6] respectively.
► The second line assigns the values 5.6 to fFloatNum[0], 5.7 to fFloatNum[1], and
so on.
► Similarly, the third line assigns the characters 'a' to chVowel[0], 'e' to chVowel[1],
and so on.
► Note again, for characters we must use the single apostrophe/quote (') to enclose them.
► Also, the last character in chVowel is NULL character ('\0').
Page 13 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arrays - Array Initialization
Page 14 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Arrays - Array Initialization
► Initialization of an array of type char for holding strings may take the following form,
► char arrayName[size] = "string_lateral_constant";
► For example, the array chVowel in the previous example could have been written more
compactly as follows,
► char chVowel[6] = "aeiou";
► When the value assigned to a character array is a string (which must be enclosed in
double quotes), the compiler automatically supplies the NULL character but we still
have to reserve one extra place for the NULL.
Page 15 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Exercises
► Write a C program to read n numbers into an array and find the sum of
all elements.
► Write a C program to find the largest and smallest elements in an array.
► Write a C program to count the number of even and odd numbers in an
array.
► Write a C program to search for a given number in an array and display
its position.
Page 16 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bubble Sort
First Pass
Page 17 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bubble Sort
Second Pass
Page 18 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bubble Sort
Third Pass
Page 19 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bubble Sort
Third Pass
Page 20 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bubble Sort
Fourth Pass
Page 21 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bubble Sort
Page 22 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bubble sort program
Page 23 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Bubble sort program
Page 24 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Two-dimensional arrays in C
Page 25 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Two-dimensional arrays in C
Page 26 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Initializing two-dimensional arrays
Page 27 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Accessing elements in two-dimensional arrays
#include <stdio.h>
int main()
{
// array declaration and initialization
int my_array[5][3] = {
{1, 2, 3}, //row 1
{4, 5, 6}, //row 2
{7, 8, 9}, //row 3
{10, 11, 12}, //row 4
{13, 14, 15} //row 5
};
Page 28 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Strings
Page 4
String and Character Array
► Remember that the C language does not support strings as a data type.
Page 5 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
String and Character Array
► For example: The string "home" contains 5 characters including the '\0'
character which is automatically added by the compiler at the end of the
string.
Page 6 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Declaring and Initializing a string variables
// valid
char name[13] = "StudyTonight";
char name[10] = {'c','o','d','e','\0'};
// Illegal
char ch[3] = "hello";
str = "hello";
Page 7 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
String Input and Output
Page 8 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
String Input and Output
► puts()
► The function puts() is used to print the string on the output stream with the
additional new line character ‘\n’.
► It moves the cursor to the next line.
► Implementation of puts() is easier than printf().
► Here is the syntax of puts() in C language,
► puts(“string”);
Page 9 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
String Handling Functions
► Hence, you must include string.h header file in your programs to use
these functions.
Page 10 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
String Handling Functions
► The following are the most commonly used string handling functions.
Method Description
strcat() It is used to concatenate(combine) two strings
strlen() It is used to show the length of a string
strrev() It is used to show the reverse of a string
strcpy() Copies one string into another
strcmp() It is used to compare two string
Page 11 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
strlen() – String Length
#include <stdio.h>
#include <string.h>
int main() {
char text[] = "Hello, C!";
printf("Length of string: %d\n", strlen(text));
return 0;
} Length of string: 9
Page 12 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
What is strcpy()?
► Parameters:
► destination – A pointer to the destination array where the source string will be
copied.
► source – A pointer to the source string that needs to be copied
► It copies the characters of the source string (including the null terminator \0) into the
destination string.
► The destination array should have enough space to store the copied string; otherwise, it
may lead to buffer overflow.
► Return Value: Returns a pointer to the destination string (destination).
Page 13 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
strcpy() - Example
#include<stdio.h>
#include<string.h>
int main() {
char source[] = "Hello, C!";
char destination[20]; // Make sure destination has enough space
strcpy(destination, source);
return 0;
} Source String: Hello, C!
Copied String: Hello, C!
Page 14 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
strncpy() – Safe String Copy
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, C!";
char destination[6];
strncpy(destination, source, 5);
destination[5] = '\0'; // Ensure null termination
printf("Copied String: %s\n", destination);
return 0; Copied String: Hello
}
Page 15 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
strcat() – String Concatenation in C
► destination: The string to which source is appended. This should have enough
space to accommodate the additional characters.
► source: The string to be appended.
► Returns: A pointer to destination after concatenation.
Page 16 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Working of strcat()
► The function finds the null terminator (\0) of the destination string.
► It starts copying characters from source to destination from the null
position. It copies all characters, including the \0 terminator of source.
#include <stdio.h>
#include <string.h>
int main() {
char str1[30] = "Hello, ";
char str2[] = "World!";
Page 17 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Memory Considerations
► The destination string must have enough space to hold both the original
content and the appended source.
► If there isn't enough space, strcat() causes buffer overflow, leading to
unpredictable behavior or program crashes.
Page 18 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Safer Alternative: strncat()
#include <stdio.h>
#include <string.h>
int main() {
char str1[12] = "Hello";
char str2[] = " World!";
return 0;
} Concatenated String: Hello Worl
Page 19 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
strcmp() and strncmp() in C – String Comparison Functions
Page 20 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
strcmp() -Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Apple";
char str2[] = "Banana";
if (result == 0)
printf("Both strings are equal.\n");
else if (result < 0)
printf("\"%s\" comes before \"%s\" in dictionary order.\n", str1, str2);
else
printf("\"%s\" comes after \"%s\" in dictionary order.\n", str1, str2);
return 0;
} "Apple" comes before "Banana" in dictionary order.
Page 21 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
strncmp() – Comparing Limited Characters
Page 22 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
strncmp() – Comparing Limited Characters
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "HelloWorld";
char str2[] = "HelloThere";
if (result == 0)
printf("First 5 characters are equal.\n");
else
printf("First 5 characters are different.\n");
return 0;
}
First 5 characters are equal.
Page 23 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
When to Use Which?
Page 24 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
typedef
Page 4
What is typedef in C?
Page 5 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Why Use typedef?
int main() {
UINT myVar = 100;
printf("Value: %u\n", myVar);
return 0;
}
Page 6 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Using typedef with Arrays
int main() {
Marks student1 = {90, 85, 88, 92, 80};
return 0;
}
Page 7 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Enumerated Data Types in C (enum)
Page 8
What is an Enumerated Data Type?
Page 9 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Example 1: Basic Enum Usage
#include <stdio.h>
int main() {
enum Days today;
today = Wednesday;
Page 10 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Example 2: Assigning Custom Values
#include <stdio.h>
int main() {
printf("SUCCESS = %d\n", SUCCESS);
printf("FAILURE = %d\n", FAILURE);
printf("PENDING = %d\n", PENDING);
return 0;
}
Output:
SUCCESS = 1
FAILURE = -1
PENDING = 0
Page 11 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Using Enum in a Switch Statement
#include <stdio.h>
int main() {
enum TrafficLight signal = YELLOW;
switch(signal) {
case RED:
printf("Stop!\n");
break;
case YELLOW:
printf("Get Ready!\n");
break;
case GREEN:
printf("Go!\n");
break;
default:
printf("Invalid Signal\n");
}
return 0;
}
Page 12 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Enum with Typedef
#include <stdio.h>
int main() {
Switch light = ON;
if (light == ON) {
printf("The light is ON.\n");
} else {
printf("The light is OFF.\n");
}
return 0;
}
Page 13 Prof. Sarju S, Department of Computer Science and Engineering, SJCET Palai
Thank You
Prof. Sarju S
Department of Computer Science and Engineering
St. Joseph’s College of Engineering and Technology, Palai (Autonomous)
sarju.s@sjcetpalai.ac.in
Page 14 Disclaimer - This document contains images/texts from various internet sources. Copyright belongs to the respective content creators.
Document is compiled exclusively for study purpose and shall not be used for commercial purpose.