Posts

Showing posts from September, 2022

C: Concatenate strings with dynamic memory.

https://github.com/pereiradaniel/c_programs/blob/master/strappend_dynmem.c // Implement string concatentation with dynamic memory #include <stdio.h> #include <string.h> // use strings #include <stdlib.h> // use calloc // returns ptr to new string on heap char *string_append(char* s1, char* s2) { // make space for new string on the heap that can store s1 and s2 // get lengths of s1 and s2 using strlen int s1l = strlen(s1); // Returns string length - null terminator int s2l = strlen(s2); int size = s1l + s2l + 1; // +1 for null terminator! // allocate space char *s = calloc(size, sizeof(char)); // copy chars from s1 into s for (int i =0; i < s1l; ++i) s[i] = s1[i]; // copy chars from s2 into s for (int i=0; i < s2l; ++i) s[s1l + i] = s2[i]; // s1l + i to shift over index! // insert null terminator s[size - 1] = '\0'; return s; } int main(int argc, char* argv[]) {

C: Use sizeof operator to get the length of an array.

  https://github.com/pereiradaniel/c_programs/blob/master/find_array_len.c // Use the size of operator to get the array length. #include <stdio.h> #include <stdlib.h> #include "length.h" // defines length of an array #define SIZE 9 // use a pre-processor directive to store the size of the array void print_size(int array[]) { int length = sizeof(array) / sizeof(array[0]); printf("LENGTH: %d\n", length); } int main(int argc, char *argv[]) { // const int size = 9; // use a const to store the size of the array int array[SIZE] = {1,2,3,4,5,6,7,8,9}; // sizeof queries size of the object type: https://devdocs.io/c/language/sizeof // Different ways to calculate the length of the array: // int length = sizeof(array) / sizeof(int); // int length = sizeof(array) / sizeof(array[0]); int length = LENGTH(array); printf("length of array: %d\n", length); printf("sizeof(array), number of bytes used

C programming and relevancy

Image
The C language is likely to remain with us for the foreseeable future. Here are a few interesting points about the C language that I was able to find while reading some posts on the topic. Links to original posts included. Another useful advantage of the C language:  C vs. Assembly In theory, I am a proponent of assembly language. In reality, I have reached a point in my life at which assembly language is a threat to both my financial security and my sanity. Writing firmware in assembly is slow and error-prone, and maintaining an adequate level of organization in long, complex programs is hopelessly difficult. However, I will certainly insist that you cannot really understand  high-level languages  if you don’t understand assembly. If you’ve never had the opportunity to gain some solid experience with assembly language, you should at least familiarize yourself with some of the basic concepts before you dive into C. The articles listed above in the Supporting Information section are a

C: Concatenate two arrays into a new 3rd array.

  https://github.com/pereiradaniel/c_programs/blob/master/conc_arrays.c // Concatenate two arrays into a new 3rd array #include <stdio.h> #include <stdlib.h> // use malloc and free #include <string.h> // memcopy #include "length.h" // Function returtns pointer to an int: // - Because space will by dynamically alloc to the heap // for the new array! // - Function will return a pointer to that array // on the heap! // In C, when an array is passed to a function, what is // really passed is a pointer to that array. What is // being passed is the first elem of the array. int* concat(int* a1, size_t len1, int *a2, size_t len2); int main (int argc, char* argv[]) { // Initialize two arrays: int array1[] = {0,1,2,3,4}; int array2[] = {5,6,7,8,9}; // Store the pointer returned by concat. int* array3 = concat(array1, LENGTH(array1), array2, LENGTH(array2)); for (int i = 0; i < 10; ++i) { printf("array3[%d] =

C: Implement bubble sort.

https://github.com/pereiradaniel/c_programs/blob/master/bubble_sort.c #include <stdio.h> #include <stdbool.h> void swap(int *x, int *y) { bool swapped = false; // compares and swaps if necessary while (*x > *y) { printf("Comparing %d and %d\n", *x, *y); int temp = *y; *y = *x; *x = temp; } return; } void bubble_sort(int a[], int length) { bool swapped = false; int i = 0; do { swapped = false; for (int j = 0; j < length - 1 - i; ++j) { swap(&a[j], &a[j+1]); swapped = true; } ++i; } while (swapped); } void display(int x[], int length) { for (int i=0; i < length; ++i) printf("%d\n", x[i]); } int main() { int a[] = {7,1,3,9,0,2,4,5,8}; int b[] = {12,345,12,456,23,34,546,2,34,5,92,74,2829,4729,273,84721,372}; int length = sizeof(a) / sizeof(int); bubble_sort(a, length); bu

C: Remove duplicate chars from a string.

  https://github.com/pereiradaniel/c_programs/blob/master/rem_dupl_char.c // Remove duplicate chars from a string. #include <stdio.h> #include <string.h> // Use string length function to find length of a string. // Create a function that removes duplicate chars after their first appearance in a string. void rem_dup(char* string) { // Find length of string: int length = strlen(string); // Returns length of str NOT including NULL term!!! // Use nested loops: // Outermost loop - Examines string one char at a time until length is reached. for (int i=0; i < length; ++i) { // Next loop checks to see if any other chars match the one at i: for (int j=i+1; j < length; ) // j is not incremented here! in case char at j needs removal { if (string[i] == string[j]) // Checks for dupl char { // Remove duplicate char from string: // Pull forward all remaining chars by 1 index.