Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
String operations in C (without using built-in methods)
/* This program is supposed to be some kind of breakthrough. In this program I wanted to challenge myself, if I can get this shit to work without bugs. Well, there's 1 problem which is the 'substring' function. So, if you are planning to use this program anywhere then make sure NOT use the 'substring' function. PROGRAM BY 'JAMES COLLINS' Thanks for reading this. */ #include <stdio.h> #include <stdlib.h> // Method to convert the entire string to lower case characters. char *to_lowerCase(char str[]); // Method to convert the entire string to upper case characters. char *to_upperCase(char str[]); // Method to get the minimum number among 2 numbers. int min(int a, int b); // Method to get index of a character. Would require linear serach that is: O(n) int char_index(char str[], char search, int index); // Method to get index of a string. Would require linear seaerch as well. int str_index(char str[], char search[], int index); // Methdo to get a substring of the given string, from the start index to the end. This is pretty easy. char *substring(char str[], int start, int end); // Method to get whether a given string ends with a certain character. 1 -> True, 0 -> False int endswith(char str[], char search, int end_index); // Method to get whether a given string ends with a certain string. 1 -> True, 0 -> False int str_endswith(char str[], char search[], int end_index); // Method to get whether a given string starts with a certain character. 1 -> True, 0 -> False int startswith(char str[], char search, int start_index); // Method to get whether a given string starts with a certain string. 1 -> True, 0 -> False int str_startswith(char str[], char search[], int start_index); // Method to remove a character from a given string. The 'remove_all' is used as a boolean if the value is 0 then it is 'false', else, it is 'true'. char *char_remove(char str[], char elem, int remove_all); // Method to compare 2 strings. 0 if they are same. 1 if the initial string is larger. -1 if the initial string is smaller. int compare(char *str, char *compare_str); // Method to get size of a string. int str_size(char *str); // Method to concatenate two strings and return another string. char *concat(char str[], char add[]); // Method to count a certain characters occurances in a string. int count(char str[], char countable); int main() { // This method is only used for testing purposes. int i = 0; char str[] = {"Hello, my name is 'James Collins'. It's an alias so the quotes. I did this program as a small challenge to see whether I am able to do this or not. Let's see if I am able to."}; char character = '.', search_string[] = {"James Collins"}, end_string[] = {"able to."}; printf("Index of the charactre \'m\': %d\n\n", char_index(str, 'm', 8)); printf("Index of the word \'James\': %d\n\n", str_index(str, "James", 0)); printf("Comparison: %d\n\n", compare(str, "Hello")); printf("Ending with a character: %d\n\n", endswith(str, '.', str_size(str))); printf("Starting with a character: %d\n\n", startswith(str, 'H', 0)); printf("Ending with a string: %d\n\n", str_endswith(str, "able to.", str_size(str))); printf("Starting with a string: %d\n\n", str_startswith(str, "Hello, ", 0)); printf("Lower Case: %s\n\n", to_lowerCase(str)); printf("Upper Case: %s\n\n", to_upperCase(str)); printf("Concatenated strings: %s\n\n", concat("This is a test for", " string concatenation. Let's see if it works.")); printf("Substring: %s\n\n", substring(str, 1, 10)); printf("Removed: %s", char_remove(str, ' ', 1)); //gets(str); return 0; } // METHOD DONE. int min(int a, int b) { return a < b ? a : b; } // METHOD DONE. int str_size(char *str) { int i; for (i = 0; str[i] != '\0'; ++i) ; return i; } // METHOD DONE. int char_index(char str[], char search, int index) { int i; for (i = index; i < str_size(str); i++) { if (str[i] == search) return i; } return -1; } // METHOD DONE. int str_index(char str[], char search[], int index) { int search_length = str_size(search); int i, end_index = str_size(str) - search_length; for (i = index; i != -1; i++) { int j, found = 1; // 'found' is being used as a 'true' or 'false' value. i = char_index(str, search[0], i); // This loop compares the required string and the contents of the given string. I don't how to exactly explain this, but I do know what I am doing. for (j = 0; j < search_length; j++) { if (search[j] != str[i + j]) // Comparing 2 characters. { found = 0; // If the characters don't match, set 'found' to 'false' and break out of the loop to save any little amount of time. break; } } if (found) return i; } return -1; } // METHOD DONE. char *substring(char str[], int start, int end) { // This is a guard clause. Here, this has been used to keep the value in //the correct order because otherwise it could cause errors that can be otherwise avoided. if (start > end) { int temp = end; end = start; start = temp; } char *ret = malloc(end - start); for (int i = start; i < end; i++) { char curr = str[i]; ret[i - start] = curr; } return ret; } // METHOD DONE. int compare(char *str, char *compare_str) { // This is a guard which checks the strings in terms of their string first. if (str_size(str) < str_size(compare_str)) return -1; else if (str_size(str) > str_size(compare_str)) return 1; int i; int size = min(str_size(str), str_size(compare_str)); for (i = 0; i < size; i++) { if (str[i] < compare_str[i]) return -1; else if (str[i] > compare_str[i]) return 1; } return 0; } // METHOD DONE. int endswith(char str[], char search, int end_index) { if (end_index <= 0) return -1; return str[end_index - 1] == search; } // METHOD DONE. int startswith(char str[], char search, int start_index) { if (start_index >= str_size(str) - 1) return -1; return str[start_index] == search ? 1 : 0; } // METHOD DONE. int str_endswith(char str[], char search[], int end_index) { if (end_index <= 0) return -1; int substring_start = end_index - str_size(search), substring_end = end_index - 1, comparison_result; int j; for (j = 0; j < substring_start - substring_end; j++) { if (str[substring_start + j] != search[j]) return 0; } return 1; } // METHOD DONE. int str_startswith(char str[], char search[], int start_index) { if (start_index >= str_size(str) - 1) return -1; int substring_end = start_index - str_size(search), substring_start = start_index, comparison_result; int j; for (j = 0; j < substring_start - substring_end; j++) { if (str[substring_start + j] != search[j]) return 0; } return 1; } // METHOD DONE. char *to_lowerCase(char str[]) { char *ret = malloc(str_size(str)); for (int i = 0; i < str_size(str); i++) { char curr = str[i]; if (curr + 0 > 65 && curr + 0 < 90) ret[i] = (char)(curr + 32); else ret[i] = curr; } return ret; } // METHOD DONE. char *to_upperCase(char str[]) { char *ret = malloc(str_size(str)); for (int i = 0; i < str_size(str); i++) { char curr = str[i]; if (curr + 0 > 96 && curr + 0 < 123) curr = (char)(curr - 32); ret[i] = curr; } return ret; } // METHOD DONE. char *concat(char str[], char add[]) { int index = 0, i; char *ret = malloc(str_size(str) + str_size(add)); for (i = 0; i < str_size(str); i++) { ret[index++] = str[i]; } for (i = 0; i < str_size(add); i++) { ret[index++] = add[i]; } return ret; } // METHOD DONE. int count(char str[], char countable) { int counter = 0, i; for (i = 0; i < str_size(str); i++) { if (str[i] == countable) counter++; } return counter; } // METHOD DONE. char *char_remove(char str[], char elem, int removeall) { if (removeall == 0) { int string_length = str_size(str), found = 0; char *first = substring(str, 0, char_index(str, elem, 0)), *second = substring(str, char_index(str, elem, char_index(str, elem, 0) + 1), str_size(str)); return concat(first, second); } int string_length = str_size(str), found = 0; char *ret = malloc(string_length - count(str, elem)); for (int i = 0; i < string_length; i++) { if (str[i] == elem) { found++; continue; } ret[i - found] = str[i]; } return ret; } // PROGRAM BY 'JAMES COLLINS'
run
|
edit
|
history
|
help
1
SAI_1-5.c
gets.c
bitmask shift
Area
HeapSort
Float data type.c
print name
c4
C_141127:MatricesUnSoloIndice
day 3 integer manipulation