Week 12 Lecture Code

#include <stdio.h>
#include <stdlib.h>


// Return the number of elements that are odd at
// corresponding indexes of both arr1 and arr2
int count_odd(int size1, int arr1[], int size2, int arr2[]) {
// if we had two lists instead of two arrays
// int count_odd(struct node *head1, struct node *head2)    
    int count = 0;
    int i = 0;
    // struct node *tmp1 = head1;
    // struct node *tmp2 = head2;
    while(i < size1 && i < size2) {   //while (tmp1 != NULL && tmp2 != NULL)
        if (arr1[i] % 2 != 0 && arr2[i] % 2 != 0) {
        // if (tmp->data % 2 != 0 && tmp2->data % 2 != 0)    
            count++;
        }
        i++;
        // tmp1 = tmp1->next;
        // tmp2 = tmp2->next;
    }
    return count;
}

int main(void) {

    // Only your function is called during testing
    // Any changes in this main function will not
    // be used in testing

    int array1[] = {9, -3, 1, 5, 2};
    int array2[] = {3, 2, -7, 6};

    printf("%d odd at the same index\n", count_odd(5, array1, 4, array2));

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

struct initials {
    char first_initial;
    char last_initial;
};

// Return the total number of structs that DON'T
// have both initials set to capital letters
int struct_capitals(int size, struct initials array[]) {
    //TODO
    // I need a loop to go to the entire array's elements
    int i = 0;
    int count = 0;
    while(i < size) {
        if (array[i].first_initial >= 'A' && array[i].first_initial <= 'Z' && 
            array[i].last_initial >= 'A' && array[i].last_initial <= 'Z') {
        // if (isupper(array[i].first_initial) &&
        //     isupper(array[i].last_initial))        
                count++;
            }
            i++;
    }
    return size - count;
}

// This is a simple main function which could be used
// to test your struct_capitals function.
// It will not be marked.
// Only your struct_capitals function will be marked.

#define TEST_ARRAY_SIZE 5

int main(void) {
    struct initials test_array[TEST_ARRAY_SIZE] = {
        { .first_initial = 'A', .last_initial = 'F'},
        { .first_initial = '!', .last_initial = 'Z'}, 
        { .first_initial = 'Z', .last_initial = 'X'},
        { .first_initial = 'N', .last_initial = 's'}, 
        { .first_initial = 't', .last_initial = 'a'} 
    };
    printf("%d\n", struct_capitals(TEST_ARRAY_SIZE, test_array));

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>

struct node {
    struct node *next;
    int          data;
};


// Given two linked lists, 
// count the number of even numbers in both linked lists 
// and return the difference (should be the absolute value). 
// For example:
// 2->1->4->X
// 1->2->3->4->100->X
// Would print out
// 1

int even_diff(struct node *head1, struct node *head2) {
    // PUT YOUR CODE HERE
    // count the number of even values in head1
    int count1 = 0;
    struct node *tmp1 = head1;
    while (tmp1 != NULL) {
        if (tmp1->data % 2 == 0) {
            count1++;
        }
        tmp1 = tmp1->next;
    }
    int count2 = 0;
    struct node *tmp2 = head2;
    while (tmp2 != NULL) {
        if (tmp2->data % 2 == 0) {
            count2++;
        }
        tmp2 = tmp2->next;
    }

    return abs (count1 - count2);
    // if (count1 > count2) {
    //      return count1 - count2;
    // } else {
    //      return count2 - count1;
    // }

}


struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    // create two linked lists from command line arguments
    int dash_arg = argc - 1;
    while (dash_arg > 0 && strcmp(argv[dash_arg], "-") != 0) {
        dash_arg = dash_arg - 1;
    }
    struct node *head1 = strings_to_list(dash_arg - 1, &argv[1]);
    struct node *head2 = strings_to_list(argc - dash_arg - 1, &argv[dash_arg + 1]);

    printf("%d\n", even_diff(head1, head2));

    return 0;
}


// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}
// To find elements in list head->6->1->7->12->NULL that are divisible by 3 run like:
// ./list_find_divisible 3 6 1 7 12
// 3

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>


struct node {
    struct node *next;
    int          data;
};

// find_divisible should return the number of items in the list
// divisible by the given value

int find_divisible(int value, struct node *head) {
    // traverse the linked list
    int count = 0;
    struct node *tmp = head;
    while (tmp != NULL) {
        if (tmp->data %  value == 0) {
            count++;
        }
        tmp = tmp->next;
    }
    return count;
}



////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    if (argc == 1) {
        printf("Usage %s value list_data\n", argv[0]);
        return 0;
    }
    // create linked list from command line arguments
    int value = atoi(argv[1]);
    struct node *head = strings_to_list(argc - 2, &argv[2]);

    // If you're getting an error here,
    // you have returned an uninitialized value
    
    printf("%d\n", find_divisible(value, head));

    return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");

    for (struct node *n = head; n != NULL; n = n->next) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
    }
    printf("]\n");
}
/*Problem 1: 

find_range takes one parameter: a pointer to the head of
the list to search through. find_range should return a single integer: 
the difference between the smallest value and the largest value in the list.

For example, if the list contained:
3->4->5->6->X

find_range should return 3, as 6 - 3 = 3

Testing
list_find_range.c</b> also contains a <b>main</b> function which
allows you to test your <b>find_range</b> function.

This main function:

converts the command-line arguments to a linked list
assigns a pointer to the first node in the linked list to head
calls find_range(head)
prints the result.

Do not change this main function.  If you want to change it, you have
misread the question.

Your find_range function will be called directly in marking.
The main function is only to let you test your find_range function

Here is how the main function allows you to test find_range:

dcc list_find_range.c -o list_find_range
./list_find_range 3 4 5
The range is: 2

./list_find_range 10 9 8 7 6 5 4 3 2 1
The range is: 9

./list_find_range
The list is empty!


Assumptions/Restrictions/Clarifications.
find_range should return only a single integer: the range of the list.
find_range should not change the linked list it is given.
find_range should not change the next or data fields of list nodes.
find_range should not use arrays.
find_range should not call malloc.
find_range should not call scanf (or getchar or fgets).
find_range should not print anything. It should not call printf.
Do not change the definition of struct node.
Do not change the supplied main function. It will not be tested or marked.
*/

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

// Do not change these #defines, or your program will fail the autotests!
#define EMPTY_LIST -42

struct node {
    struct node *next;
    int          data;
};

// find_range should return the range of the list, or
// EMPTY_LIST if the list is empty.

int find_range(struct node *head) {
    // if list is empty
    if (head == NULL) {
        return EMPTY_LIST;
    }

    // find max and min
    int max = head->data;
    int min = head->data;
    struct node *tmp = head;
    while (tmp != NULL) {
        if (max < tmp->data) {
            max = tmp->data;
        }
        if (min > tmp->data) {
            min = tmp->data;
        }
        tmp = tmp->next;

    }
    
    return max - min;
}


////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////

int find_range(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = strings_to_list(argc - 1, &argv[1]);

    // If you're getting an error here,
    // you have returned an uninitialized value
    int range = find_range(head);
    if (range == EMPTY_LIST) {
        printf("The list is empty!\n");
    } else {
        printf("The range is: %d\n", range);
    }

    return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}
#include <stdio.h>
#include <stdlib.h>

#define NUM_ROW 4
#define NUM_COL 4

// sum_min should return the sum of
// the minimum values in each row of the array
int min_row_sum(int size, int array[NUM_ROW][NUM_COL]) {

    int sum = 0;
    // outer loop: process all the rows
    int row = 0;
    while (row < NUM_ROW) {

        // find the smallest value in this row
        int min = array[row][0];
        int col = 1;
        while (col < NUM_COL) {
            if (array[row][col] < min) {
                min = array[row][col];
            }
            col++;
        }
        // min should have the smallest value from
        // the row
        sum += min;
        row++;
    }

    return sum;
}

// This is a simple main function which could be used
// to test your sum_min function.
// It will not be marked.
// Only your sum_min function will be marked.
int main(void) {
    int test_array[NUM_ROW][NUM_COL] = {
        {1, 2, 3, 4}, // row 0, find min, check all the cols
        {4, 2, 2, 0},
        {9, 9, 9, 9},
        {5, 2, 5, 5}
    };

    int result = min_row_sum(NUM_COL, test_array);
    printf("%d\n", result);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>


// max_subarray_sum should return the largest contiguous sum
// of a subarray in a given array
// eg
// For an array with elements 8, 3, 5, 4, -1, 3, -5, -4
// you would get 22 from elements 8 + 3 + 5 + 4 + -1 + 3

int max_subarray_sum(int size, int array[]) {
    int max_sum = array[0];
    int start = 0;
    while (start < size) {
        int stop = start;
        while (stop < size) {

            // start and stop are now set correctly
            // calculate the sum
            int sum = 0;
            int i = start;
            while (i <= stop) {
                sum += array[i];
                i++;
            }
            // is this the biggest sum??
            if (sum > max_sum) {
                max_sum = sum;
            }
            stop++;
        }
        start++;
    }
    return max_sum;
}

// This is a simple main function which could be used
// to test your sum_cont_subarray function.
// It will not be marked.
// Only your sum_cont_subarray function will be marked.
int main(void) {
    int test_array[] = {8, 3, 5, 4, -3, 3, -5, -4};

    int max_sum = max_subarray_sum(8, test_array);
    printf("%d\n", max_sum);

    return 0;
}
#include <stdio.h>

#define MAX_SIZE 10000

// reads integers into an array from terminal until a number is entered 
// which when multiplied by another number previously entered 
// results in 56. 
// ./array_multiplied
// 2
// 3
// 28
// 28 * 2 = 56

// ./array_multiplied
// 99
// 4
// 1
// 7
// 56
// 56 * 1 = 56

#define TARGET 56

int main(void) {
    // what type of read loop do we need?
    int numbers[MAX_SIZE];
    int i = 0;

    while (i < MAX_SIZE) {
        // read in the next number
        scanf("%d", &numbers[i]);

        // check if the product of this number and any of
        // the previous ones is 56. if so, we're done.

        int j = 0;
        while (j < i) {
            // check if numbers[i] * numbers[j] == 56
            if (numbers[i] * numbers[j] == TARGET) {
                printf("%d * %d = %d\n", numbers[i],
                    numbers[j], numbers[i] * numbers[j]);
                return 0;
            }
            j++;
        }
        i++;
    }

    // will only happen if run out of array space
    return 0;
}
// Read integers until a negative integer is read 
// and then print odd and even integers on separate lines
// Assume a max of 1000 integers will be read before the first negative input
// e.g
// $ ./array_odd_even 
// 1 
// 2 
// 3 
// 2
// -42 
// Odd numbers were: 1 3 
// Even numbers were: 2 2 


#include <stdio.h>

#define MAX_NUMBERS 1000

int main(void) {

    int odd[MAX_NUMBERS];
    int even[MAX_NUMBERS];
    int odd_count = 0;
    int even_count = 0;

    // data entry loop
    // what am i reading in? (integers/lines?)
    // do i need to check for EOF? no
    int number;
    scanf("%d", &number);
    while (number >= 0) {
        if (number % 2 == 0) {
            // even: add to even array
            even[even_count] = number;
            even_count++;
        } else {
            odd[odd_count] = number;
            odd_count++;
        }
        scanf("%d", &number);
    }

    printf("Odd numbers were:");
    int i = 0;
    while (i < odd_count) {
        printf(" %d", odd[i]);
        i++;
    }
    printf("\n");

    printf("Even numbers were:");
    i = 0;
    while (i < even_count) {
        printf(" %d", even[i]);
        i++;
    }
    printf("\n");

    return 0;
}
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>


struct node {
    struct node *next;
    int          data;
};

// delete_div should delete the first node that is divisible by the given value 
// If there are no nodes that are divisible by the given value, it should return
// the list unchanged.
struct node *delete_div(int value, struct node *head) {

    if (head != NULL && head->data % value == 0) {
        // edge case: delete the first node
        struct node *new_head = head->next;
        free(head);
        return new_head;
    }

    struct node *prev = NULL;
    struct node *curr = head;
    while (curr != NULL && curr->data % value != 0) {
        prev = curr;
        curr = curr->next;
    }

    // maybe we didn't find it
    if (curr != NULL) {
        // delete this node
        prev->next = curr->next;
        free(curr);
    }

    return head;
}



////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    if (argc == 1) {
        printf("Usage %s value list_data\n", argv[0]);
        return 0;
    }
    // create linked list from command line arguments
    int value = atoi(argv[1]);
    struct node *head = strings_to_list(argc - 2, &argv[2]);

    // If you're getting an error here,
    // you have returned an uninitialized value
    struct node *new_head = delete_div(value, head);
    print_list(new_head);

    return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");

    for (struct node *n = head; n != NULL; n = n->next) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
    }
    printf("]\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
    struct node *next;
    int          data;
};


// Delete the first instance of a duplicate in the given list 
// 1->3->5->3->1->X 
// should return 
// 1->3->5->3->X
struct node *delete_duplicate(struct node *head) {
    struct node *tmp = head;
    while(tmp != NULL) {
        // check if there is a duplicate of tmp->data and delete it and return
        struct node * curr = tmp->next;
        struct node *prev = tmp;
        while (curr != NULL) {
            if (tmp->data == curr->data) {
                // delete it
                prev->next = curr->next;
                free(curr);
                curr = prev->next;
                return head;
            } else {
                prev = curr;
                curr = curr->next;
            }
        }
        tmp = tmp->next;
    }

    return head;
}


// DO NOT CHANGE CODE BELOW

struct node *strings_to_list(int len, char *strings[]);
void print_list(struct node *head);


int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = NULL;
    if (argc > 1) {
        // list has elements
        head = strings_to_list(argc - 1, &argv[1]);
    }

    struct node *new_head = delete_duplicate(head);
    print_list(new_head);

    return 0;
}


// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    int i = len - 1;
    while (i >= 0) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
        i -= 1;
    }   
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");    
    struct node *n = head;
    while (n != NULL) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
        n = n->next;
    }
    printf("]\n");
}
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

struct node {
  struct node *next;
  char data;
};

 
// duplicate should duplicate every node in the list, inserting the new
// node after the original node
// a z c d d..... a a z z c c d d d d
struct node *duplicate(struct node *head) { 
	struct node *tmp = head;
	while(tmp != NULL) {
		// make the new node
		struct node *new = malloc(sizeof(struct node));
		new->data = tmp->data;
		new->next = tmp->next;
		tmp->next = new;
		tmp = tmp->next->next;

	}
	
	
	return head;
}

////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
	// create linked list from command line arguments
	struct node *head = strings_to_list(argc - 1, &argv[1]);

	// If you're getting an error here,
	// you have returned an uninitialized value
	struct node *new_head = duplicate(head);
	print_list(new_head);

	return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
	struct node *head = NULL;
  	for (int i = len - 1; i >= 0; i = i - 1) {
		struct node *n = malloc(sizeof(struct node));
		assert(n != NULL);
		n->next = head;
		n->data = strings[i][0];
		head = n;
  	}
  	return head;
}

// print linked list
void print_list(struct node *head) {
	printf("[");

  	for (struct node *n = head; n != NULL; n = n->next) {
    	// If you're getting an error here,
   	 	// you have returned an invalid list
    	printf("%c", n->data);
    	if (n->next != NULL) {
      		printf(", ");
    	}
  	}
  	printf("]\n");
}
// To find elements in list 6->1->7->12->NULL that are divisible by 3 run like:
// ./list_find_divisible 3 6 1 7 12
// 2

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>


struct node {
    struct node *next;
    int          data;
};

// find_divisible should return the number of items in the list
// divisible by the given value

int find_divisible(int value, struct node *head) {
    int count = 0;
    struct node *curr = head;
    while (curr != NULL) {

        if (curr->data % value == 0) {
            count++;
        }
        curr = curr->next;
    }
    return count;
}



////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    if (argc == 1) {
        printf("Usage %s value list_data\n", argv[0]);
        return 0;
    }
    // create linked list from command line arguments
    int value = atoi(argv[1]);
    struct node *head = strings_to_list(argc - 2, &argv[2]);

    // If you're getting an error here,
    // you have returned an uninitialized value
    
    printf("%d\n", find_divisible(value, head));

    return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");

    for (struct node *n = head; n != NULL; n = n->next) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
    }
    printf("]\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
    struct node *next;
    int          data;
};

// Insert a new node into a sorted linked list, maintaining
// the sorted order
// 3 7 10 16 (insert 8)..... 3 7 8 10 16
struct node *insert_sorted(struct node *head, int number) {

    // create the new node
    struct node *new_node = malloc(sizeof(struct node));
    new_node->data = number;
    new_node->next = NULL;

    struct node *prev = NULL;
    struct node *curr = head;
    while (curr != NULL && curr->data < number) {
        prev = curr;
        curr = curr->next;
    }

    // edge case: inserting at the head (includes empty list)
    if (prev == NULL) {
        // insert at the head
        new_node->next = head;
        head = new_node;
    } else {
        // insert the new node between prev and curr
        prev->next = new_node;
        // prev->next->next = curr; // this should work
        new_node->next = curr;   // same as above
    }

    return head;
}


// DO NOT CHANGE THE CODE BELOW

struct node *strings_to_list(int len, char *strings[]);
void print_list(struct node *head);


int main(int argc, char *argv[]) {   

    if (argc == 1) {
        printf("Usage %s value list_data\n", argv[0]);
        return 0;
    }
    // create linked list from command line arguments
    int value = atoi(argv[1]);
    struct node *head = strings_to_list(argc - 2, &argv[2]);

    struct node *new_head = insert_sorted(head, value);
    print_list(new_head);

    return 0;
}


// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    int i = len - 1;
    while (i >= 0) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
        i -= 1;
    }   
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");    
    struct node *n = head;
    while (n != NULL) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
        n = n->next;
    }
    printf("]\n");
}
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {

    // If no arguments (only program name)
    if (argc == 1) {
        printf("There are no arguments\n");
        return 0;
    }

    int max_count = 0;
    char *most_common = NULL;

    // Compare each argument with others
    for (int i = 1; i < argc; i++) {
        int count = 0;

        for (int j = 1; j < argc; j++) {
            if (strcmp(argv[i], argv[j]) == 0) {
                count++;
            }
        }

        if (count > max_count) {
            max_count = count;
            most_common = argv[i];
        }
    }

    printf("Most common argument is %s and it occurs %d times\n",
        most_common, max_count);

    return 0;
}
// An isogram is a word, in which no letter of the alphabet 
// occurs more than once. Write a C program that reads in 
// strings until Ctrl+D, and checks whether the string is an isogram. 

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX 100

// Sample input/output

// hello
// This is not an isogram
// table
// This is an isogram
// Yay
// This is not an isogram
// CtrlD

int main(void) {
    char line[MAX];

    while (fgets(line, MAX, stdin) != NULL) {
        // count the letters
        int letters[26] = {0};
        int isogram = 1;
        int i = 0;
        while (line[i] != '\0') {
            if (isalpha(line[i])) {
                int index = tolower(line[i]) - 'a';
                letters[index]++;
                if (letters[index] > 1) {
                    isogram = 0;
                }
            }
            i++;
        }

        if (isogram == 0) {
            printf("This is not an isogram.\n");
        } else {
            printf("This is an isogram.\n");
        }
    }
}
// Write a C program that reads in words from standard input until Ctrl+D, 
// and checks whether a word read in as a command line argument is a substring 
// in the word. If so, then the word that was read in should be printed again


// Assume max length of strings read is is 100 characters

// $./array_substring courage age bloom encourage 
// blooming 
// blooming
// potato
// encourage
// encourage
// rage
// rage
// egg

#include <stdio.h>
#include <string.h>

#define MAX 100

int main(int argc, char *argv[]) {

    char line[MAX];
    while (fgets(line, MAX, stdin) != NULL) {
        int len = strlen(line);
        if (len > 0 && line[len - 1] == '\n') {
            len--;
        }

        int found = 0;
        int i = 1;
        while (i < argc) {
            // is argv[i] a substring of line?
            int arg_len = strlen(argv[i]);
            int j = 0;
            while (j <= (len - arg_len)) {
                if (strncmp(&line[j], argv[i], arg_len) == 0) {
                    found = 1;
                }
                j++;
            }
            i++;
        }
        if (found == 1) {
            printf("%s", line);
        }
    }
    return 0;
}


 
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
    struct node *next;
    int          data;
};

// You want to traverse a linked list and 
// find the smallest number in it 
// For example, 20, 4, 3, 6 
// Would return 3
// return -1 if list is empty
int smallest(struct node *head) {    
    
    if (head == NULL) {
        return -1;
    }
    int min = head->data;
    struct node *curr = head;
    while (curr != NULL) {
        if (curr->data < min) {
            min = curr->data;
            
        }
        curr = curr->next;
    }
    return min;
}

struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION

int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = strings_to_list(argc - 1, &argv[1]);

    printf("%d\n", smallest(head));

    return 0;
}


// DO NOT CHANGE THIS FUNCTION

// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}
// The following code is meant to take two string inputs from the user and
// concatenate them together. However, there are a number of mistakes in the
// code that are causing it to not work as intended. Can you find and fix the
// errors? Good luck!

// YOU MAY NOT USE strcat

#include <stdio.h>
#include <string.h>

void strip_newline(char *string);

int main(void) {
    char str1[50];
    char str2[50];
    char concat[100];
    int length1;
    int length2;

    printf("Enter the first string: ");
    fgets(str1, 50, stdin);
    strip_newline(str1);

    printf("Enter the second string: ");
    fgets(str2, 50, stdin);
    strip_newline(str2);

    length1 = strlen(str1);
    length2 = strlen(str2);

    if (length1 + length2 > 100) {
        printf("Error: Strings are too long to concatenate\n");
        return 1;
    }
    
    for (int i = 0; i < length1; i++) {
        concat[i] = str1[i];
    }
    
    for (int i = length1; i < length1 + length2; i++) {
        concat[i] = str2[i - length1];
    }
    concat[length1 + length2] = '\0';

    printf("%s\n", concat);

    return 0;
}

// Removes newline from end of the given string if it exists
// NOTE: This function is correct as given.
// You do not need to modify it for this question.
void strip_newline(char *string) {
    int length = strlen(string);
    if (length != 0 && string[length - 1] == '\n') {
        string[length - 1] = '\0';
    }
}
#include <stdio.h>
#include <stdlib.h>


// Return the number of elements that are odd at
// corresponding indexes of both arr1 and arr2
int count_odd(int size1, int arr1[], int size2, int arr2[]) {
    return -42;
}

int main(void) {

    // Only your function is called during testing
    // Any changes in this main function will not
    // be used in testing

    int array1[] = {9, -3, 1, 5, 2};
    int array2[] = {3, 2, -7, 6};

    printf("%d odd at the same index\n", count_odd(5, array1, 4, array2));

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

struct node {
    struct node *next;
    int          data;
};


// Given two linked lists, 
// count the number of even numbers in both linked lists 
// and return the difference (should be the absolute value). 
// For example:
// 2->1->4->X
// 1->2->3->4->100->X
// Would print out
// 100

int even_diff(struct node *head1, struct node *head2) {
    // PUT YOUR CODE HERE

    return -42;

}


struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    // create two linked lists from command line arguments
    int dash_arg = argc - 1;
    while (dash_arg > 0 && strcmp(argv[dash_arg], "-") != 0) {
        dash_arg = dash_arg - 1;
    }
    struct node *head1 = strings_to_list(dash_arg - 1, &argv[1]);
    struct node *head2 = strings_to_list(argc - dash_arg - 1, &argv[dash_arg + 1]);

    printf("%d\n", even_diff(head1, head2));

    return 0;
}


// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}
// To find elements in list 6->1->7->12->NULL that are divisible by 3 run like:
// ./list_find_divisible 3 6 1 7 12
// 2

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>


struct node {
    struct node *next;
    int          data;
};

// find_divisible should return the number of items in the list
// divisible by the given value

int find_divisible(int value, struct node *head) {

    return 42;
}



////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    if (argc == 1) {
        printf("Usage %s value list_data\n", argv[0]);
        return 0;
    }
    // create linked list from command line arguments
    int value = atoi(argv[1]);
    struct node *head = strings_to_list(argc - 2, &argv[2]);

    // If you're getting an error here,
    // you have returned an uninitialized value
    
    printf("%d\n", find_divisible(value, head));

    return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");

    for (struct node *n = head; n != NULL; n = n->next) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
    }
    printf("]\n");
}
/*Problem 1: 

find_range takes one parameter: a pointer to the head of
the list to search through. find_range should return a single integer: 
the difference between the smallest value and the largest value in the list.

For example, if the list contained:
3->4->5->6->X

find_range should return 3, as 6 - 3 = 3

Testing
list_find_range.c</b> also contains a <b>main</b> function which
allows you to test your <b>find_range</b> function.

This main function:

converts the command-line arguments to a linked list
assigns a pointer to the first node in the linked list to head
calls find_range(head)
prints the result.

Do not change this main function.  If you want to change it, you have
misread the question.

Your find_range function will be called directly in marking.
The main function is only to let you test your find_range function

Here is how the main function allows you to test find_range:

dcc list_find_range.c -o list_find_range
./list_find_range 3 4 5
The range is: 2

./list_find_range 10 9 8 7 6 5 4 3 2 1
The range is: 9

./list_find_range
The list is empty!


Assumptions/Restrictions/Clarifications.
find_range should return only a single integer: the range of the list.
find_range should not change the linked list it is given.
find_range should not change the next or data fields of list nodes.
find_range should not use arrays.
find_range should not call malloc.
find_range should not call scanf (or getchar or fgets).
find_range should not print anything. It should not call printf.
Do not change the definition of struct node.
Do not change the supplied main function. It will not be tested or marked.
*/

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

// Do not change these #defines, or your program will fail the autotests!
#define EMPTY_LIST -42

struct node {
    struct node *next;
    int          data;
};

// find_range should return the range of the list, or
// EMPTY_LIST if the list is empty.

int find_range(struct node *head) {

    return 42;
}


////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////

int find_range(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = strings_to_list(argc - 1, &argv[1]);

    // If you're getting an error here,
    // you have returned an uninitialized value
    int range = find_range(head);
    if (range == EMPTY_LIST) {
        printf("The list is empty!\n");
    } else {
        printf("The range is: %d\n", range);
    }

    return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}
#include <stdio.h>
#include <stdlib.h>

#define NUM_ROW 4
#define NUM_COL 4

// sum_min should return the sum of
// the minimum values in each row of the array
int min_row_sum(int size, int array[NUM_ROW][NUM_COL]) {
    
    return 42;
}

// This is a simple main function which could be used
// to test your sum_min function.
// It will not be marked.
// Only your sum_min function will be marked.
int main(void) {
    int test_array[NUM_ROW][NUM_COL] = {
        {1, 2, 3, 4}, {4, 2, 2, 0}, {9, 9, 9, 9}, {5, 2, 5, 5}};

    int result = min_row_sum(NUM_COL, test_array);
    printf("%d\n", result);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>


// max_subarray_sum should return the largest contiguous sum
// of a subarray in a given array
// eg
// For an array with elements 8, 3, 5, 4, -1, 3, -5, -4
// you would get 22 from elements 8 + 3 + 5 + 4 + -1 + 3

int max_subarray_sum(int size, int array[]) {
     
    return 42;
}

// This is a simple main function which could be used
// to test your sum_cont_subarray function.
// It will not be marked.
// Only your sum_cont_subarray function will be marked.
int main(void) {
    int test_array[] = {8, 3, 5, 4, -1, 3, -5, -4};

    int max_sum = max_subarray_sum(8, test_array);
    printf("%d\n", max_sum);

    return 0;
}
#include <stdio.h>

#define MAX_SIZE 10000

// reads integers into an array from terminal until a number is entered 
// which when multiplied by another number previously entered 
// results in 56. 
// ./array_multiplied
// 2
// 3
// 28
// 28 * 2 = 56

// ./array_multiplied
// 99
// 4
// 1
// 7
// 56
// 56 * 1 = 56

int main(void) {

    return 0;
}
// Read integers until a negative integer is read 
// and then print odd and even integers on separate lines
// Assume a max of 1000 integers will be read before the first negative input
// e.g
// $ ./array_odd_even 
// 1 
// 2 
// 3 
// 2
// -42 
// Odd numbers were: 1 3 
// Even numbers were: 2 2 


#include <stdio.h>

#define MAX_NUMBERS 1000
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>


struct node {
    struct node *next;
    int          data;
};

// delete_div should delete the first node that is divisible by the given value 
// If there are no nodes that are divisible by the given value, it should return
// the list unchanged.
struct node *delete_div(int value, struct node *head) {

    return NULL;
}



////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    if (argc == 1) {
        printf("Usage %s value list_data\n", argv[0]);
        return 0;
    }
    // create linked list from command line arguments
    int value = atoi(argv[1]);
    struct node *head = strings_to_list(argc - 2, &argv[2]);

    // If you're getting an error here,
    // you have returned an uninitialized value
    struct node *new_head = delete_div(value, head);
    print_list(new_head);

    return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");

    for (struct node *n = head; n != NULL; n = n->next) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
    }
    printf("]\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
    struct node *next;
    int          data;
};


// Delete the first instance of a duplicate in the given list 
// 1->3->5->3->1->X 
// should return 
// 1->3->5->3->X
struct node *delete_duplicate(struct node *head) {

    return NULL;
}


// DO NOT CHANGE CODE BELOW

struct node *strings_to_list(int len, char *strings[]);
void print_list(struct node *head);


int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = NULL;
    if (argc > 1) {
        // list has elements
        head = strings_to_list(argc - 1, &argv[1]);
    }

    struct node *new_head = delete_duplicate(head);
    print_list(new_head);

    return 0;
}


// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    int i = len - 1;
    while (i >= 0) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
        i -= 1;
    }   
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");    
    struct node *n = head;
    while (n != NULL) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
        n = n->next;
    }
    printf("]\n");
}
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

struct node {
  struct node *next;
  char data;
};

 
// duplicate should duplicate every node in the list, inserting the new
// node after the original node
// a z c d d..... a a z z c c d d d d
struct node *duplicate(struct node *head) { 
    // TODO - change the return value


	
	return NULL;
}

////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
	// create linked list from command line arguments
	struct node *head = strings_to_list(argc - 1, &argv[1]);

	// If you're getting an error here,
	// you have returned an uninitialized value
	struct node *new_head = duplicate(head);
	print_list(new_head);

	return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
	struct node *head = NULL;
  	for (int i = len - 1; i >= 0; i = i - 1) {
		struct node *n = malloc(sizeof(struct node));
		assert(n != NULL);
		n->next = head;
		n->data = strings[i][0];
		head = n;
  	}
  	return head;
}

// print linked list
void print_list(struct node *head) {
	printf("[");

  	for (struct node *n = head; n != NULL; n = n->next) {
    	// If you're getting an error here,
   	 	// you have returned an invalid list
    	printf("%c", n->data);
    	if (n->next != NULL) {
      		printf(", ");
    	}
  	}
  	printf("]\n");
}
// To find elements in list 6->1->7->12->NULL that are divisible by 3 run like:
// ./list_find_divisible 3 6 1 7 12
// 2

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>


struct node {
    struct node *next;
    int          data;
};

// find_divisible should return the number of items in the list
// divisible by the given value

int find_divisible(int value, struct node *head) {

    return 42;
}



////////////////////////////////////////////////////////////////////////
//               DO NOT CHANGE THE CODE BELOW                         //
////////////////////////////////////////////////////////////////////////
void print_list(struct node *head);
struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION
int main(int argc, char *argv[]) {
    if (argc == 1) {
        printf("Usage %s value list_data\n", argv[0]);
        return 0;
    }
    // create linked list from command line arguments
    int value = atoi(argv[1]);
    struct node *head = strings_to_list(argc - 2, &argv[2]);

    // If you're getting an error here,
    // you have returned an uninitialized value
    
    printf("%d\n", find_divisible(value, head));

    return 0;
}

// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");

    for (struct node *n = head; n != NULL; n = n->next) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
    }
    printf("]\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
    struct node *next;
    int          data;
};

// Insert a new node into a sorted linked list, maintaining
// the sorted order
// 3 7 10 16 (insert 8)..... 3 7 8 10 16 
struct node *insert_sorted(struct node *head, int number) {
    // PUT YOUR CODE HERE, REMEMBER TO CHANGE
    // THE RETURN
    
    return NULL;
}


// DO NOT CHANGE THE CODE BELOW

struct node *strings_to_list(int len, char *strings[]);
void print_list(struct node *head);


int main(int argc, char *argv[]) {   

    if (argc == 1) {
        printf("Usage %s value list_data\n", argv[0]);
        return 0;
    }
    // create linked list from command line arguments
    int value = atoi(argv[1]);
    struct node *head = strings_to_list(argc - 2, &argv[2]);

    struct node *new_head = insert_sorted(head, value);
    print_list(new_head);

    return 0;
}


// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    int i = len - 1;
    while (i >= 0) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
        i -= 1;
    }   
    return head;
}

// print linked list
void print_list(struct node *head) {
    printf("[");    
    struct node *n = head;
    while (n != NULL) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
        n = n->next;
    }
    printf("]\n");
}
#include <stdio.h>
#include <string.h>


int main(int argc, char *argv[]) {

    // INSERT CODE IN HERE

   return 0;
}
// An isogram is a word, in which no letter of the alphabet 
// occurs more than once. Write a C program that reads in 
// strings until Ctrl+D, and checks whether the string is an isogram. 

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX 100

// Sample input/output

// hello
// This is not an isogram
// table
// This is an isogram
// Yay
// This is not an isogram
// CtrlD

int main(void) {

    return 0;
}
// Write a C program that reads in words from standard input until Ctrl+D, 
// and checks whether a word read in as a command line argument is a substring 
// in the word. If so, then the word that was read in should be printed again


// Assume max length of strings read is is 100 characters

// $./array_substring courage age bloom encourage 
// blooming 
// blooming
// potato
// encourage
// encourage
// rage
// rage
// egg

#include <stdio.h>
#include <string.h>

#define MAX 100

int main(int argc, char *argv[]) {

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct node {
    struct node *next;
    int          data;
};

// You want to traverse a linked list and 
// find the largest number in it 
// For example, 20, 4, 3, 6 
// Would return 3
// return -1 if list is empty
int smallest(struct node *head) {    
    int min;
    if (head == NULL) {
        min = -1;
    }
    int min = head->data;
    struct node *curr = head;
    while (curr->next != NULL) {
        if (head->data < min) {
            min = head->data;
            curr = curr->next;
        }
    }
    return min;
}

struct node *strings_to_list(int len, char *strings[]);

// DO NOT CHANGE THIS MAIN FUNCTION

int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = strings_to_list(argc - 1, &argv[1]);

    printf("%d\n", smallest(head));

    return 0;
}


// DO NOT CHANGE THIS FUNCTION

// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    for (int i = len - 1; i >= 0; i = i - 1) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
    }
    return head;
}
// The following code is meant to take two string inputs from the user and
// concatenate them together. However, there are a number of mistakes in the
// code that are causing it to not work as intended. Can you find and fix the
// errors? Good luck!

// YOU MAY NOT USE strcat

#include <stdio.h>
#include <string.h>

void strip_newline(char *string);

int main(void) {
    char str1[50];
    char str2[50];
    char concat[100];
    int length1;
    int length2;

    printf("Enter the first string: ");
    fgets(str1, 50);
    strip_newline(str1);

    printf("Enter the second string: ");
    fgets(str1, 50);
    strip_newline(str2);

    length1 = strlen(str1);
    length2 = strlen(str2);

    if (lenth1 + lengh2 >= 100) {
        printf("Error: Strings are too long to concatenate\n");
        return 1;
    }
    
    for (int i = 0; i <= len1 + len2; i++) {
        concat[i] = str1[i];
    }
    
    for (int i = len1; i <= len2; i++) {
        concat[i] = str2[i];
    }

    printf("%c\n", concat[0]);

    return 0;
}

// Removes newline from end of the given string if it exists
// NOTE: This function is correct as given.
// You do not need to modify it for this question.
void strip_newline(char *string) {
    int length = strlen(string);
    if (length != 0 && string[length - 1] == '\n') {
        string[length - 1] = '\0';
    }
}


    printf("Enter the first string: ");
    fgets(str1, 50, stdin);          // ✅ FIX: added stdin
    strip_newline(str1);             // remove newline if present

    printf("Enter the second string: ");
    fgets(str2, 50, stdin);          // ✅ FIX: read into str2 (not str1)
    strip_newline(str2);             // ✅ FIX: strip newline from str2

    length1 = strlen(str1);          // get length of first string
    length2 = strlen(str2);          // get length of second string

    // ✅ FIX: corrected variable names (length1, length2)
    if (length1 + length2 >= 100) {  
        printf("Error: Strings are too long to concatenate\n");
        return 1;
    }
    
    // copy characters from str1 into concat
    for (int i = 0; i < length1; i++) {   // ✅ FIX: only loop over str1 length
        concat[i] = str1[i];
    }
    
    // append characters from str2 after str1
    for (int i = 0; i < length2; i++) {   // ✅ FIX: proper indexing
        concat[length1 + i] = str2[i];
    }

    // ✅ FIX: add null terminator at the end of new string
    concat[length1 + length2] = '\0';

    // ✅ FIX: print full string (not just one character)
    printf("%s\n", concat);

    return 0;
}

// Removes newline from end of the given string if it exists
// NOTE: This function is correct as given.
void strip_newline(char *string) {
    int length = strlen(string);
    if (length != 0 && string[length - 1] == '\n') {
        string[length - 1] = '\0';
    }
}