mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add first-non-repeating-character/c/function
This commit is contained in:
parent
108642e57f
commit
5f7ba3b5ae
@ -0,0 +1,3 @@
|
|||||||
|
# first-non-repeating-character/c/function
|
||||||
|
|
||||||
|
Created by [@Divlo](https://github.com/Divlo) on 8 October 2021.
|
@ -0,0 +1,10 @@
|
|||||||
|
#include "character.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void character_append(char* string, char character) {
|
||||||
|
size_t length = strlen(string);
|
||||||
|
string[length] = character;
|
||||||
|
string[length + 1] = '\0';
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
#ifndef CHARACTER_H
|
||||||
|
#define CHARACTER_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Append a character to a string, assuming string points to an array
|
||||||
|
* with enough space.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* @param character
|
||||||
|
*/
|
||||||
|
void character_append(char* string, char character);
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,19 @@
|
|||||||
|
#include "input.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "character.h"
|
||||||
|
|
||||||
|
char* input() {
|
||||||
|
char character;
|
||||||
|
size_t length = 1;
|
||||||
|
char* string = malloc(length * sizeof(char));
|
||||||
|
*string = '\0';
|
||||||
|
while ((character = getchar()) != '\n' && character != EOF) {
|
||||||
|
length++;
|
||||||
|
string = realloc(string, length * sizeof(char));
|
||||||
|
character_append(string, character);
|
||||||
|
}
|
||||||
|
return string;
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
#ifndef INPUT_H
|
||||||
|
#define INPUT_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Read a line from stdin.
|
||||||
|
*
|
||||||
|
* @return char*
|
||||||
|
*/
|
||||||
|
char* input();
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,19 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "character.h"
|
||||||
|
#include "input.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char* string = input();
|
||||||
|
char character = string_first_non_repeating_character(string);
|
||||||
|
if (character == 0) {
|
||||||
|
printf("\n");
|
||||||
|
} else {
|
||||||
|
printf("%c\n", character);
|
||||||
|
}
|
||||||
|
free(string);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "character.h"
|
||||||
|
|
||||||
|
size_t string_total_occurrences_of_character(char* string, char character) {
|
||||||
|
size_t result = 0;
|
||||||
|
size_t string_length = strlen(string);
|
||||||
|
for (size_t index = 0; index < string_length; index++) {
|
||||||
|
char current_character = string[index];
|
||||||
|
if (current_character == character) {
|
||||||
|
result += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
char string_first_non_repeating_character(char* string) {
|
||||||
|
size_t string_length = strlen(string);
|
||||||
|
char result = ""[0];
|
||||||
|
for (size_t index = 0; index < string_length; index++) {
|
||||||
|
char character = string[index];
|
||||||
|
if (string_total_occurrences_of_character(string, character) == 1) {
|
||||||
|
result = character;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef STRING_H
|
||||||
|
#define STRING_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns the total number of occurrences of the given character in the string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* @param character
|
||||||
|
* @return size_t
|
||||||
|
*/
|
||||||
|
size_t string_total_occurrences_of_character(char* string, char character);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Returns the first non-repeating character in the given string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* @return char*
|
||||||
|
*/
|
||||||
|
char string_first_non_repeating_character(char* string);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user