C Programming Module5
C Programming Module5
MODULE 5
Strings: String taxonomy, operations on strings, Miscellaneous string and character
functions, arrays of strings.
Pointers: Understanding the Computers Memory, Introduction to Pointers, Declaring
Pointer Variables
Structures: Introduction to structures.
String Taxonomy
a) strcat()
Definition: The strcat() function concatenates (appends) one string to the end of another.
Syntax:
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
String handling functions in C allow efficient manipulation of strings. These functions are
defined in the <string.h>header file. Below are explanations and examples of some commonly
used string handling functions:
b) strcpy()
Definition: The strcpy() function copies the content of one string into another.
Syntax:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50];
char str2[] = "Copy this string.";
strcpy(str1, str2);
c) strcmp()
Definition: The strcmp() function compares two strings lexicographically.
Syntax:
int strcmp(const char *str1, const char *str2);
Returns:
• 0 if both strings are equal.
• A negative value if str1 is less than str2.
• A positive value if str1 is greater than str2.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
if (result == 0) {
printf("Strings are equal.\n");
}
else {
printf("str1 is not equal to str2.\n");
}
return 0;
}
Output
str1 is not equal to str2.
d) strlen()
The strlen() function in C is used to calculate the length of a string (number of characters in the
string excluding the null-terminator \0). It is defined in the <string.h> library.
Syntax:
size_t strlen(const char *str);
• Parameter:
str - A pointer to the string whose length is to be calculated.
• Return Value: The function returns the length of the string as a size_t type.
Example Program:
#include <stdio.h>
#include <string.h>
int main() {
char string[] = "Hello, world!";
printf("Length: %zu\n", strlen(string));
return 0;
}
Output: Length: 13
#include <stdio.h>
int main() {
char str[100];
int length = 0;
return 0;
}
Output:
Enter a string: This is a string
The length of the string is: 16
Program:
#include <stdio.h>
int main() {
char str[100];
int i;
return 0;
}
Output:
Program:
#include <stdio.h>
int main() {
char str[100];
int i, vowels = 0, consonants = 0;
Program:
#include <stdio.h>
int main() {
char str[100];
int i, vowels = 0, digits = 0, spaces = 0, special = 0;
return 0;
}
Enter a string: Hello friends! How are you doing? Today is Jan 19 2025.
Total vowels: 15
Total digits: 6
Total blank spaces: 10
Total special characters: 3
Write a C program to check whether the given string is palindrome or not without using
Library functions.
Program:
#include <stdio.h>
int main() {
char str[100];
int i, length = 0, isPalindrome = 1;
return 0;
}
Output:
Sample 1:
Enter a string: madam
The given string is a palindrome.
Write a C program to count the number of lines, words and characters in a given text.
Program:
#include <stdio.h>
int main() {
char text[1000];
int i, lines = 0, words = 0, characters = 0;
int inWord = 0;
if (text[i] == '\n') {
lines++; // Count lines
}
return 0;
}
Output:
Enter the text (end input with ~):
What should I enter?
Enough for now~
Number of characters: 35
Number of words: 7
Number of lines: 2
Program:
#include <stdio.h>
int main() {
char str[100], temp;
int i, length = 0;
// Input the string
printf("Enter a string: ");
scanf("%s", str);
Output:
Enter a string: DEVIL
Reversed string: LIVED
A pointer in C is a variable that stores the memory address of another variable. Pointers are a
powerful feature of C that enable direct access and manipulation of memory.
Syntax:
data_type *pointer_name;
• data_type specifies the type of data the pointer will point to.
• * is used to declare a pointer.
• pointer_name is the name of the pointer variable.
Significance of Pointers
• Direct Memory Access:
Pointers provide a way to access and manipulate memory directly, which is essential
for low-level programming tasks like working with hardware or managing memory.
• Efficient Memory Usage:
Pointers allow dynamic memory allocation using functions like malloc and free,
making programs more flexible and efficient.
• Pass by Reference:
When a pointer to a variable is passed to a function, the function can modify the original
variable. This avoids copying large data structures and allows direct changes.
• Array and String Handling:
Pointers can be used to traverse arrays or strings efficiently without needing additional
indexing.
• Dynamic Data Structures:
Data structures like linked lists, trees, and graphs rely on pointers to dynamically link
nodes and elements.
• Function Pointers:
Pointers can store the address of a function, enabling the creation of callback functions
and dynamic function calls.
#include <stdio.h>
int main() {
int a = 10; // A regular integer variable
int *ptr = &a; // Pointer to the variable 'a'
Output:
Value of a: 10
Address of a: 0x7ffee9ba3d94
Value of ptr: 0x7ffee9ba3d94
Value at ptr: 10
New value of a: 20
• Dynamic memory allocation: allocate memory at runtime using malloc, calloc, etc.
• Pass by reference: pass variables to functions without copying them, enabling
modification of the original value.
• Efficient array and string handling: access array elements without explicit indexing.
• Data structures: build complex data structures like linked lists, trees, and graphs.
• Access to Hardware and Low-Level Programming: Pointers enable direct
manipulation of memory addresses, which is crucial for systems programming,
embedded systems, and hardware interactions.
A pointer variable is declared by specifying the data type of the variable it will point to,
followed by an asterisk (*), and then the pointer's name.
Syntax
data_type *pointer_name;
• data_type: The type of data the pointer will point to (e.g., int, float, char, etc.).
• *: Indicates that the variable is a pointer.
• pointer_name: The name of the pointer variable.
#include <stdio.h>
int main() {
int a = 10; // Declare an integer variable
int *ptr = &a; // Declare a pointer to an integer and assign the address of 'a'
Explanation
1. int a = 10;
o Declares an integer variable a with a value of 10.
2. int *ptr = &a;
o Declares a pointer ptr that can hold the address of an integer.
o Assigns the address of a to ptr using the address-of operator (&).
3. Accessing Values and Addresses:
o &a: The address of variable a.
o ptr: The pointer ptr holds the memory address of a.
o *ptr: Dereferences the pointer ptr to get the value stored at the memory address
it points to.
Output:
Value of a: 10
Address of a: 0x7ffee9d3babc // Memory address (example)
Pointer ptr points to: 0x7ffee9d3babc
Value at ptr: 10
Example:
#include <stdio.h>
int main() {
int num = 42; // Step 1: Declare a variable
int *ptr = # // Step 2: Declare and initialize the pointer with the address
return 0;
}
Program:
#include <stdio.h>
int main() {
int num1, num2, sum;
int *ptr1, *ptr2; // Declare pointers
return 0;
}
Output:
Enter the first number: 10
Enter the second number: 20
The sum of 10 and 20 is: 30
int main() {
int a = 10, b = 5;
int *ptr1 = &a, *ptr2 = &b; // Initialize pointers
// Print results
printf("Values: a = %d, b = %d\n", *ptr1, *ptr2);
printf("Sum: %d + %d = %d\n", *ptr1, *ptr2, sum);
printf("Difference: %d - %d = %d\n", *ptr1, *ptr2, difference);
printf("Product: %d * %d = %d\n", *ptr1, *ptr2, product);
printf("Quotient: %d / %d = %d\n", *ptr1, *ptr2, quotient);
return 0;
}
Output:
Values: a = 10, b = 5
Sum: 10 + 5 = 15
Difference: 10 - 5 = 5
Product: 10 * 5 = 50
Quotient: 10 / 5 = 2
Write a function to swap the values of two variables without using a temporary variable, using
pointers.
Program:
#include <stdio.h>
int main() {
int a, b;
return 0;
}
Output:
Enter first number: 10
Enter second number: 20
Before swap:
a = 10, b = 20
After swap:
a = 20, b = 10
Develop a program using pointers to compute the sum, mean and standard deviation of
all elements stored in an array of N real numbers.
Program:
#include <stdio.h>
#include <math.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
float arr[n];
printf("Enter %d real numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%f", &arr[i]);
}
return 0;
}
Output:
Enter the number of elements: 5
Enter 5 real numbers:
24759
Sum: 27.00
Mean: 5.40
Standard Deviation: 2.42
A structure in C is a user-defined data type that allows you to group different types of
variables under a single name. These variables, known as members or fields, can have
different data types (e.g., int, float, char). Structures help organize complex data in a
meaningful way.
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
// Declare a structure variable of type 'Student'
struct Student student1;
Student Information:
Name: Rajnikanth
Age: 40
Marks: 100.00
// Structure declaration
struct Student {
char name[50];
int age;
float marks;
};
int main() {
// Assign values to structure members
student1.age = 20;
student2.age = 22;
int main() {
// Assign values to structure members
student1.age = 20;
student2.age = 22;
// Structure declaration
struct Student {
char name[50];
int age;
float marks;
};
int main() {
// Declaring and initializing structure variables
struct Student student1 = {"John", 20, 85.5};
struct Student student2 = {"Alice", 22, 90.0};
return 0;
}
#include <stdio.h>
// Structure definition
struct Date {
int day;
int month;
int year;
};
int main() {
// Declare and initialize two structure variables
struct Date date1 = {15, 10, 2024}; // 15th October 2024
struct Date date2 = {20, 01, 2025}; // 20th January 2025
return 0;
}
Output:
Date 1: 15/10/2024
Date 2: 19/01/2025
Program:
#include <stdio.h>
// Structure definition
struct Time {
int hour;
int minute;
int second;
};
int main() {
// Declare two structure variables
struct Time time1, time2;
return 0;
}
Output:
Enter hour, minute, and second for time1 (hh mm ss): 10 30 20
Enter hour, minute, and second for time2 (hh mm ss): 11 40 31
Time 1: 10:30:20
Time 2: 11:40:31