From: Jérôme Benoit Date: Wed, 15 Feb 2017 22:46:37 +0000 (+0100) Subject: exo3: implement the primaries features requested X-Git-Url: https://git.piment-noir.org/?p=TD_C.git;a=commitdiff_plain;h=63647b824ee1caecc2dac7cc7a80773c6a97cf71 exo3: implement the primaries features requested Signed-off-by: Jérôme Benoit --- 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; +}