From 63647b824ee1caecc2dac7cc7a80773c6a97cf71 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 15 Feb 2017 23:46:37 +0100 Subject: [PATCH] exo3: implement the primaries features requested MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- exo3/Makefile | 31 +++++++++++++++++++++++++++++++ exo3/exo3.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 exo3/Makefile create mode 100644 exo3/exo3.c diff --git a/exo3/Makefile b/exo3/Makefile new file mode 100644 index 0000000..c31747a --- /dev/null +++ b/exo3/Makefile @@ -0,0 +1,31 @@ +TARGET = exo3 +LIBS = +CC = gcc +# Enforce C11 ISO standard for now +CFLAGS = -std=c11 -g -Wall +LDFLAGS = -g -Wall + +.PHONY: default all clean + +default: $(TARGET) +all: default + +OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) +HEADERS = $(wildcard *.h) + +%.o: %.c $(HEADERS) + $(CC) $(CFLAGS) -c $< -o $@ + +.PRECIOUS: $(TARGET) $(OBJECTS) + +$(TARGET): $(OBJECTS) + $(CC) $(OBJECTS) $(LDFLAGS) $(LIBS) -o $@ + +clean: + -rm -f $(TARGET) $(OBJECTS) + +disassemble: $(TARGET) + objdump -d $< | less + +symbols: $(TARGET) + objdump -t $< | sort | less diff --git a/exo3/exo3.c b/exo3/exo3.c new file mode 100644 index 0000000..d1b0956 --- /dev/null +++ b/exo3/exo3.c @@ -0,0 +1,38 @@ +#include + +//FIXME: Comment the code !!! + +void swap(char* v1, char* v2) { + if (v1 != v2) { + char tmp = *v1; + *v1 = *v2; + *v2 = tmp; + } +} + +int stringLength(const char* str) { + int length; + + for(length = 0; str[length] != '\0'; ++length); + return length; +} + +// FIXME: this function have issues with non english characters +void reverseString(char* str) { + int length = stringLength(str); + + for (int i = length - 1; i >= length/2; --i) { + swap(&str[i], &str[(length - 1) - i]); + } +} + +int main() { + char msg[] = "Bonjour et a bientot"; + int length = stringLength(msg); + + printf("La chaine de caracteres est \"%s\" et a pour longueur %d caractere(s)\n", msg, length); + reverseString(msg); + printf("La chaine inversee de caracteres est \"%s\"\n", msg); + + return 0; +} -- 2.34.1