From 9d25fee1011a36be39551774fcf8471e38504beb Mon Sep 17 00:00:00 2001 From: caskd Date: Sat, 30 Nov 2019 16:24:56 +0100 Subject: [PATCH] Update progress to exercise 5 --- CMakeLists.txt | 2 +- chapters/2/exercises/5.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 chapters/2/exercises/5.c diff --git a/CMakeLists.txt b/CMakeLists.txt index ec17789..15a3f77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.0) project(learningc LANGUAGES C) set(CHAPTER 2) -set(EXERCISE 4) +set(EXERCISE 5) add_executable(learningc chapters/${CHAPTER}/exercises/${EXERCISE}.c) #target_link_libraries(learningc m) # Required for 2-3 diff --git a/chapters/2/exercises/5.c b/chapters/2/exercises/5.c new file mode 100644 index 0000000..7943036 --- /dev/null +++ b/chapters/2/exercises/5.c @@ -0,0 +1,19 @@ +#include +int any(char s1[], char s2[]); +int main() { + printf("%i\n",any("tester","r")); + return 0; +} + +int any(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]) { + return ++k; + } + } + } + return -1; +}