Computer Programming 1 PDF
Document Details
Tags
Summary
This document provides examples of computer programming concepts in C focusing on arrays and strings, including initialization, accessing, modifying strings, and functions.
Full Transcript
int i = 0; do { // Access array[i] i++; } while (i < array_size); Example: int numbers = {10, 20, 30, 40, 50}; int i = 0; do { printf("%d\n", numb...
int i = 0; do { // Access array[i] i++; } while (i < array_size); Example: int numbers = {10, 20, 30, 40, 50}; int i = 0; do { printf("%d\n", numbers[i]); // Prints each element of the array i++; } while (i < 5); 3. Basic operations on arrays (e.g., finding the maximum, summing elements) A. Lab Exercise 1: Write a program to find the largest element in an array. B. Lab Exercise 2: Develop a program that reverses the elements of an array. Week 9: Strings and Text Processing 1. Working with Strings in C Strings in C are arrays of characters terminated by a null character ('\0'). They are used to represent sequences of characters, such as words or sentences. a. Defining Strings Syntax: char string_name[string_size]; Example: char name; // Declares a character array of size 20 Initialization: Strings can be initialized with a string literal, which automatically adds the null terminator. Syntax: char string_name[] = "string_literal"; Example: char greeting[] = "Hello, World!"; // Initializes a string with a literal b. Accessing and Modifying Strings Accessing Characters: Characters in a string can be accessed using array indexing. Example: char name[] = "Alice"; printf("%c\n", name); // Prints 'A' Modifying Strings: Strings can be modified by accessing individual characters. Example: char name[] = "Alice"; name = 'B'; // Changes the string to "Blice" IT112 – Instructional Material in Computer Programming 1 28 c. Standard Library Functions for Strings The C Standard Library provides a set of functions to handle string operations. These functions are defined in the header. Common Functions: strlen(): Returns the length of the string (excluding the null terminator). size_t length = strlen("Hello"); // length = 5 strcpy(): Copies a string to another. char dest; strcpy(dest, "Hello"); // Copies "Hello" to dest strcat(): Concatenates (appends) one string to another. char dest = "Hello"; strcat(dest, " World"); // Concatenates " World" to dest, resulting in "Hello World" strcmp(): Compares two strings. int result = strcmp("Hello", "World"); // Returns a negative value because "Hello" < "World" strchr(): Finds the first occurrence of a character in a string. char *ptr = strchr("Hello", 'e'); // ptr points to the 'e' in "Hello" strstr(): Finds the first occurrence of a substring in a string. char *ptr = strstr("Hello World", "World"); // ptr points to "World" in "Hello World" 1. Performing common string operations (e.g., concatenation, comparison) a. String Concatenation Concatenation involves appending one string to another. This is achieved using the strcat() function. Example: #include #include int main() { char str1 = "Hello"; char str2[] = " World"; strcat(str1, str2); // Appends str2 to str1 printf("%s\n", str1); // Prints "Hello World" return 0; } Explanation: strcat appends the content of str2 to str1, and str1 now contains "Hello World". b. String Comparison Comparison involves comparing two strings lexicographically (i.e., based on dictionary order). This is achieved using the strcmp() function. Example: IT112 – Instructional Material in Computer Programming 1 29 #include #include int main() { char str1[] = "Apple"; char str2[] = "Banana"; int result = strcmp(str1, str2); if (result < 0) { printf("%s comes before %s\n", str1, str2); } else if (result > 0) { printf("%s comes after %s\n", str1, str2); } else { printf("%s is equal to %s\n", str1, str2); } return 0; } Explanation: strcmp returns a negative value if str1 is lexicographically less than str2, a positive value if str1 is greater, and zero if they are equal. c. String Length Finding the length of a string (excluding the null terminator) is done using the strlen() function. Example: #include #include int main() { char str[] = "Hello, World!"; size_t length = strlen(str); printf("Length of the string: %zu\n", length); // Prints "Length of the string: 13" return 0; } Explanation: strlen returns the number of characters in the string, excluding the null terminator. 2.String formatting and manipulation A. Lab Exercise 1: Write a program to check if a given string is a palindrome. B. Lab Exercise 2: Develop a program to count the number of vowels in a string. Week 10: Midterm Exam Midterm Exam covering Weeks 1-9 topics Week 11: Introduction to Files 1. Understanding file concepts in C In C, files are used to store data persistently. File operations are fundamental for tasks such as reading from and writing to files. Here’s an overview of file concepts in C: 1. File Streams Definition: A file stream is a sequence of characters that flows to or from a file. In C, files are managed using file streams provided by the standard library. IT112 – Instructional Material in Computer Programming 1 30