├── README.md ├── main.h ├── src ├── count_vowels.c ├── remove_duplicates.c ├── remove_spaces.c ├── remove_str_duplicates.c └── rotate_array.c └── test ├── count_vowels_test.c ├── remove_duplicates_test.c ├── remove_spaces_test.c ├── remove_str_duplicates_test.c └── rotate_array_test.c /README.md: -------------------------------------------------------------------------------- 1 | ### Hi there 👋 2 | ![115946188-cf20fe00-a4d4-11eb-811a-4b9d8f91fa34](https://github.com/yadi09/coding-challenge_one/assets/140100340/ce00f70c-556e-4697-a014-d57ea982e898) 3 | # coding-challenge_one 4 | This repository was created for practicing coding, and it concerns what we have learned in the ALX software engineering program. 5 | 6 | 7 | ############ coding challenge(question) that include pointer, array, string, function, in c progaramming language ##### 8 | 9 | 10 | 11 | #### follow the following steps #### 12 | 13 | 1. Add some discription about yourself to README.md file, at the root of the folder of the project like: 14 | * Your Name: 15 | * UserName: 16 | * tell as about yourself: 17 | - 18 | - 19 | * social media links: 20 | - 21 | - 22 | * and add also discribtion of the project 23 | - 24 | - 25 | 2. Clone the repository to your local computer. in the repository and config your email < git config user.gmail > use that use also for github account 26 | 3. create a branch in your local commputer and working on that (to protect main branch). 27 | 6. Complete the tasks on your local computer within the new branch and push it to the repository. 28 | 7. and on your github account request pull (pull request). (to merge to main branch if you sure about your code) 29 | 8. done..........................get started (if you have any question or confusion let me know) 30 | 31 | # 32 | # 33 | # 34 | # 35 | # 36 | #### Coding-Challenge_One ###### 37 | # 38 | 1, Challenge: Count Vowels 39 | 40 | Write a C function countVowels that accepts a string as an argument and returns the count of vowels (a, e, i, o, u, A, E, I, O, U) present in the string. 41 | and also create your test main file 42 | 43 | * Prototype: int countVowels( char* str); 44 | * Example: ------------- 45 | 46 | --Input: "Hello, World!" 47 | 48 | --Output: 3 49 | # 50 | # 51 | # 52 | # 53 | # 54 | # 55 | 56 | 2, Challenge: Remove Duplicates from Array 57 | 58 | Write a C function removeDuplicates that accepts an array of integers and its size as arguments. 59 | The function should modify the original array by removing any duplicate elements and return the new size of the 60 | array without duplicates. 61 | and also create your test main file and push it to repo 62 | 63 | * Prototype: int removeDuplicates(int* arr, int size); 64 | 65 | * Example: ------ 66 | 67 | --int arr[] = {1, 2, 3, 2, 4, 3, 5}; 68 | 69 | --int size = 7; 70 | 71 | --int newSize = removeDuplicates(arr, size); 72 | 73 | --// arr should be {1, 2, 3, 4, 5} 74 | 75 | --// newSize should be 5 76 | # 77 | # 78 | # 79 | # 80 | # 81 | 3, Challenge: Remove Spaces from String 82 | 83 | Write a C function removeSpaces that accepts a string as an argument and removes all the spaces from it. The function should modify the original string in-place using pointers. 84 | and also create your test main file and push it to repo 85 | 86 | * Prototype: void removeSpaces(char* str); 87 | * Example: ----- 88 | 89 | --char str[] = "Hello, World! How are you?"; 90 | 91 | --removeSpaces(str); 92 | 93 | --// str should be "Hello,World!Howareyou?" 94 | # 95 | # 96 | # 97 | # 98 | # 99 | 4, Challenge: Remove Duplicates from String 100 | 101 | Write a C function removeDuplicates that accepts a string as an argument and removes any duplicate characters from it. The function should modify the original string in-place using pointers. 102 | and also create your test main file and push it to repo 103 | 104 | * Prototype: void removeDuplicates(char* str); 105 | * Example: ----- 106 | 107 | --char str[] = "Hello, World!"; 108 | 109 | --removeDuplicates(str); 110 | 111 | --// str should be "Helo, Wrd!" 112 | # 113 | # 114 | # 115 | # 116 | # 117 | # 118 | 5, Challenge: Array Rotation 119 | 120 | Write a C function rotateArray that accepts an array of integers and its size, along with a rotation count. 121 | The function should rotate the elements of the array to the right by the given rotation count in-place using pointers. 122 | and also create your test main file and push it to repo 123 | 124 | * Prototype: void rotateArray(int* arr, int size, int rotationCount); 125 | * Example: ---- 126 | 127 | --int arr[] = {1, 2, 3, 4, 5}; 128 | 129 | --int size = 5; 130 | 131 | --int rotationCount = 2; 132 | 133 | --rotateArray(arr, size, rotationCount); 134 | 135 | --// arr should be {4, 5, 1, 2, 3} 136 | # 137 | # 138 | ######################## Good luck, guys! I hope you will invite me to code with you again soon. ######################## 139 | -------------------------------------------------------------------------------- /main.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_H 2 | #define MAIN_H 3 | 4 | int count_vowels(char *str); 5 | int remove_duplicates(int* arr, int size); 6 | void remove_spaces(char *str); 7 | void remove_str_duplicates(char *str); 8 | void reverse_array(int *arr, int size); 9 | void rotate_array(int *arr, int size, int rotation_count); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /src/count_vowels.c: -------------------------------------------------------------------------------- 1 | /** 2 | * count_vowels - Counts the number of vowels (both lowercase and uppercase) in a given string. 3 | * @str: The input string to count vowels from. 4 | * 5 | * Return: The count of vowels in the string. 6 | * 7 | * Description: This function takes a null-terminated string as input and counts the occurrences 8 | * of vowels (a, e, i, o, u, A, E, I, O, U) within the string. It returns the total count of vowels found. 9 | */ 10 | 11 | int count_vowels(char *str) { 12 | char vowels[10] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}; 13 | int counter = 0; 14 | 15 | while (*str != '\0') { 16 | for (int i = 0; i < 10; i++) { 17 | if (*str == vowels[i]) { 18 | counter++; 19 | break; 20 | } 21 | } 22 | str++; 23 | } 24 | 25 | return counter; 26 | } 27 | -------------------------------------------------------------------------------- /src/remove_duplicates.c: -------------------------------------------------------------------------------- 1 | /** 2 | * remove_duplicates - Removes duplicate elements from an integer array. 3 | * 4 | * @arr: Pointer to the integer array. 5 | * @size: The size of the array. 6 | * 7 | * Return: The new size of the array after removing duplicates. 8 | * 9 | * Description: This function takes an integer array and its size as input and removes duplicate 10 | * elements from the array. It returns the new size of the array after removing duplicates. 11 | * 12 | * The function iterates through the input array, keeping track of a new array without 13 | * duplicates. It checks each element against the elements already processed and 14 | * includes only the first occurrence of each element in the new array. 15 | */ 16 | 17 | int remove_duplicates(int *arr, int size) { 18 | int new_size= 0; 19 | 20 | for (int i = 0; i < size; i++) { 21 | int is_duplicate = 0; 22 | 23 | for (int j = 0; j < new_size; j++) { 24 | if (arr[i] == arr[j]) { 25 | is_duplicate = 1; 26 | break; 27 | } 28 | } 29 | 30 | if (!is_duplicate) { 31 | arr[new_size] = arr[i]; 32 | new_size++; 33 | } 34 | } 35 | 36 | return new_size; 37 | } 38 | -------------------------------------------------------------------------------- /src/remove_spaces.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * remove_spaces - Removes spaces from a given string. 5 | * @str: Pointer to the input string. 6 | * 7 | * Description: This function takes a null-terminated string as input and removes all spaces 8 | * from the string in-place. It modifies the original string to remove spaces. 9 | * The resulting string will have no spaces, and its length may be shortened. 10 | * 11 | */ 12 | 13 | void remove_spaces(char *str) { 14 | int len = strlen(str); 15 | int i = 0; 16 | 17 | for (int j = 0; j < len - 1; j++) { 18 | if (str[j] == ' ') 19 | continue; 20 | str[i] = str[j]; 21 | i++; 22 | } 23 | 24 | str[i] = '\0'; 25 | } 26 | -------------------------------------------------------------------------------- /src/remove_str_duplicates.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /** 4 | * remove_str_duplicates - Removes duplicate characters from a given string. 5 | * @str: Pointer to the input string. 6 | * 7 | * Description: This function takes a null-terminated string as input and removes duplicate 8 | * characters from the string in-place. It modifies the original string to remove 9 | * duplicates, keeping only the first occurrence of each character. The resulting 10 | * string will have no duplicate characters, and its length may be shortened. 11 | */ 12 | 13 | void remove_str_duplicates(char *str) { 14 | int len = strlen(str); 15 | int index = 0; 16 | 17 | for (int i = 0; i < len; i++) { 18 | int is_duplicate = 0; 19 | 20 | for (int j = 0; j < index; j++) { 21 | if (str[i] == str[j]) { 22 | is_duplicate = 1; 23 | break; 24 | } 25 | } 26 | 27 | if (!is_duplicate) { 28 | str[index] = str[i]; 29 | index++; 30 | } 31 | } 32 | 33 | str[index] = '\0'; 34 | } 35 | -------------------------------------------------------------------------------- /src/rotate_array.c: -------------------------------------------------------------------------------- 1 | /** 2 | * reverse_array - Reverses the elements of an integer array in-place. 3 | * @arr: Pointer to the integer array. 4 | * @size: The size of the array. 5 | * 6 | * Description: This function takes an integer array and its size as input and reverses the 7 | * order of elements in the array in-place. After execution, the elements in the 8 | * array will be reversed. 9 | */ 10 | 11 | void reverse_array(int *arr, int size) { 12 | for (int i = 0; i < size - 1; i++) { 13 | int temp = arr[i]; 14 | arr[i] = arr[size - 1]; 15 | arr[size - 1] = temp; 16 | 17 | size--; 18 | } 19 | } 20 | 21 | /** 22 | * rotate_array - Rotates the elements of an integer array to the right by a given count. 23 | * @arr: Pointer to the integer array. 24 | * @size: The size of the array. 25 | * @rotation_count: The number of positions to rotate the array to the right. 26 | * 27 | * Description: This function takes an integer array, its size, and a rotation count as input 28 | * and rotates the elements of the array to the right by the specified count. 29 | * The function performs the rotation in-place. 30 | */ 31 | 32 | void rotate_array(int* arr, int size, int rotation_count) { 33 | rotation_count = rotation_count % size; 34 | 35 | reverse_array(arr, size); 36 | reverse_array(arr, rotation_count); 37 | reverse_array(arr + rotation_count, size - rotation_count); 38 | } 39 | -------------------------------------------------------------------------------- /test/count_vowels_test.c: -------------------------------------------------------------------------------- 1 | #include "../main.h" 2 | #include 3 | 4 | int main(void) { 5 | printf("%d\n", count_vowels("Hello, World!")); // 3 6 | printf("%d\n", count_vowels("The qUick brOwn fox jumped over the lazy dog")); // 12 7 | printf("%d\n", count_vowels("COUNT VOWELS")); // 4 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /test/remove_duplicates_test.c: -------------------------------------------------------------------------------- 1 | #include "../main.h" 2 | #include 3 | 4 | int main() { 5 | int arr1[8] = {1, 2, 2, 1, 3, 6, 7, 6}; 6 | int arr2[5] = {5, 4, 3, 2, 1}; 7 | int arr3[6] = {1, 1, 1, 1, 1, 1}; 8 | int arr4[7] = {1, 2, 3, 4, 6, 6, 1}; 9 | 10 | int size1 = remove_duplicates(arr1, 8); // 5 11 | int size2 = remove_duplicates(arr2, 5); // 5 12 | int size3 = remove_duplicates(arr3, 6); // 1 13 | int size4 = remove_duplicates(arr4, 7); // 5 14 | 15 | printf("%d\n", size1); 16 | printf("%d\n", size2); 17 | printf("%d\n", size3); 18 | printf("%d\n", size4); 19 | 20 | printf("arr1: "); 21 | // 1 2 3 6 7 22 | for (int i = 0; i < size1; i++) { 23 | printf("%d ", arr1[i]); 24 | } 25 | printf("\n"); 26 | 27 | printf("arr2: "); 28 | // 5 4 3 2 1 29 | for (int i = 0; i < size2; i++) { 30 | printf("%d ", arr2[i]); 31 | } 32 | printf("\n"); 33 | 34 | printf("arr3: "); 35 | // 1 36 | for (int i = 0; i < size3; i++) { 37 | printf("%d ", arr3[i]); 38 | } 39 | printf("\n"); 40 | 41 | printf("arr4: "); 42 | // 1 2 3 4 6 43 | for (int i = 0; i < size4; i++) { 44 | printf("%d ", arr4[i]); 45 | } 46 | printf("\n"); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /test/remove_spaces_test.c: -------------------------------------------------------------------------------- 1 | #include "../main.h" 2 | #include 3 | 4 | int main(void) { 5 | char str1[] = "Hello, World! How are you?"; 6 | char str2[] = " "; 7 | char str3[] = "cat "; 8 | 9 | remove_spaces(str1); 10 | remove_spaces(str2); 11 | remove_spaces(str3); 12 | 13 | printf("%s$\n%s$\n%s$\n", str1, str2, str3); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /test/remove_str_duplicates_test.c: -------------------------------------------------------------------------------- 1 | #include "../main.h" 2 | #include 3 | 4 | int main(void) { 5 | char str1[] = "Hello, World!"; // Helo, Wrd! 6 | char str2[] = "19fourty9"; // 19fourty 7 | char str3[] = "remove the 2 spaces"; // remov th2spac 8 | 9 | remove_str_duplicates(str1); 10 | remove_str_duplicates(str2); 11 | remove_str_duplicates(str3); 12 | 13 | printf("%s\n%s\n%s\n", str1, str2, str3); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /test/rotate_array_test.c: -------------------------------------------------------------------------------- 1 | #include "../main.h" 2 | #include 3 | 4 | int main() { 5 | // Test Case 1: Rotate by 2 positions 6 | int arr1[] = {1, 2, 3, 4, 5}; 7 | int size1 = 5; 8 | int rotation_count1 = 2; 9 | 10 | rotate_array(arr1, size1, rotation_count1); 11 | 12 | for (int i = 0; i < size1; i++) { 13 | printf("%d ", arr1[i]); 14 | } 15 | 16 | printf("\n"); 17 | 18 | // Test Case 2: Rotate by 0 positions (no change) 19 | int arr2[] = {10, 20, 30, 40, 50}; 20 | int size2 = 5; 21 | int rotation_count2 = 0; 22 | 23 | rotate_array(arr2, size2, rotation_count2); 24 | 25 | for (int i = 0; i < size2; i++) { 26 | printf("%d ", arr2[i]); 27 | } 28 | 29 | printf("\n"); 30 | 31 | // Test Case 3: Rotate by more than array size 32 | int arr3[] = {7, 8, 9}; 33 | int size3 = 3; 34 | int rotation_count3 = 10; 35 | 36 | rotate_array(arr3, size3, rotation_count3); 37 | 38 | for (int i = 0; i < size3; i++) { 39 | printf("%d ", arr3[i]); 40 | } 41 | 42 | printf("\n"); 43 | 44 | return 0; 45 | } 46 | --------------------------------------------------------------------------------