From 1ff7f327e3fe837b1658f9b10163a0cd13a3381d Mon Sep 17 00:00:00 2001 From: caskd Date: Wed, 20 Nov 2019 21:28:44 +0100 Subject: [PATCH] Uncomment required inclusion for 2-3 and update progress to 3-4 --- CMakeLists.txt | 4 ++-- chapters/2/exercises/4.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 chapters/2/exercises/4.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a5860f..ec17789 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,9 +3,9 @@ cmake_minimum_required(VERSION 3.0) project(learningc LANGUAGES C) set(CHAPTER 2) -set(EXERCISE 3) +set(EXERCISE 4) add_executable(learningc chapters/${CHAPTER}/exercises/${EXERCISE}.c) -target_link_libraries(learningc m) # Required for 2-3 +#target_link_libraries(learningc m) # Required for 2-3 install(TARGETS learningc RUNTIME DESTINATION bin) diff --git a/chapters/2/exercises/4.c b/chapters/2/exercises/4.c new file mode 100644 index 0000000..262d08c --- /dev/null +++ b/chapters/2/exercises/4.c @@ -0,0 +1,25 @@ +#include + +void squeeze(char s1[], char s2[]); +int main() { + char sent[] = "I am a ssssnake!", banned[] = "as"; + squeeze(sent,banned); + printf("Result: %s", sent); + return 0; +} +void squeeze(char s1[], char s2[]) { + int i,j,k,s; // i is writing position and k is reading position + for (i = 0, k = 0; s1[k] != '\0'; k++) { + for (s = 0, j = 0; s2[j] != '\0'; j++) { + // We check if we have a disallowed character and break if it is the case + if (s1[k] == s2[j]) { + s = 1; + break; + } + } + if (!s) { + s1[i++] = s1[k]; // As long as the character is not disallowed, write it and increase the write position + } + } + s1[i] = '\0'; +}