diff --git a/exercism/acronym/acronym.c b/exercism/acronym/acronym.c new file mode 100644 index 00000000..828b925c --- /dev/null +++ b/exercism/acronym/acronym.c @@ -0,0 +1,87 @@ +#include +#include +#include + +char *abbreviate(const char *phrase) +{ + char str[80]; + strcpy(str, phrase); + char *p_str = str; + static char acr[80]; + strcpy(acr, ""); + + /* for counting the words */ + int counter = 0; + + /* for position the words */ + int index = 0; + + /* for -loop variable */ + int i = 0; + + /* + counts the empty-characters. + for determine the number of words + */ + while (p_str && (i < 80)) + { + if (*p_str == ' ') + { + counter++; + } + if (i < 80) + { + p_str++; + i++; + } + } + + i = 0; + counter++; + char words[counter][80]; + + /* initalizes words-array with empty strings */ + for (i = 0; i < counter; i++) + { + strcpy(words[i],""); + } + + /* rewind string */ + p_str = str; + + char *p_start = p_str; + + /* collects each word in array 'words' */ + while (p_str && (i <= 80)) + { + if (*p_str == ' ') + { + *p_str = '\0'; + strncat(words[index], p_start, 80); + index++; + p_start = p_str + 1; + } + + if (i <= 80) + { + p_str++; + i++; + } + } + + /* adds the last word */ + *p_str = '\0'; + strncat(words[index], p_start, 80); + index++; + + /* builds the actual acronym */ + for (i = 0; i < index; i++) + { + /* capitalize the first character */ + words[i][0] = toupper(words[i][0]); + words[i][1] = '\0'; + strcat(acr, words[i]); + } + + return acr; +} \ No newline at end of file diff --git a/exercism/acronym/acronym.h b/exercism/acronym/acronym.h new file mode 100644 index 00000000..02eaeea2 --- /dev/null +++ b/exercism/acronym/acronym.h @@ -0,0 +1,6 @@ +#ifndef ACRONYM_H +#define ACRONYM_H + +char *abbreviate(const char *phrase); + +#endif diff --git a/exercism/hello-world/hello_world.c b/exercism/hello-world/hello_world.c new file mode 100644 index 00000000..bd922123 --- /dev/null +++ b/exercism/hello-world/hello_world.c @@ -0,0 +1,7 @@ +#include "hello_world.h" + +const char *hello(void) +{ + /* string is pointer of the first character */ + return "Hello, World!"; +} diff --git a/exercism/hello-world/hello_world.h b/exercism/hello-world/hello_world.h new file mode 100644 index 00000000..e553b802 --- /dev/null +++ b/exercism/hello-world/hello_world.h @@ -0,0 +1,6 @@ +#ifndef HELLO_WORLD_H +#define HELLO_WORLD_H + +const char *hello(void); + +#endif diff --git a/exercism/isogram/isogram.c b/exercism/isogram/isogram.c new file mode 100644 index 00000000..a54e3c81 --- /dev/null +++ b/exercism/isogram/isogram.c @@ -0,0 +1,46 @@ +#include +#include + +/* + is_isogram: returns true if the given string a isogram, otherwise false. +*/ +bool is_isogram(const char phrase[]) +{ + + /* use 'unsigned' because of the function strlen(...) */ + unsigned int i = 0; + unsigned int j = 0; + + /* the current read character in the first for-loop */ + char current_char = ' '; + + /* return status */ + bool status = true; + + /* contains the length of the given string */ + unsigned int len_phrase = strlen(phrase); + + for (i = 0; i < len_phrase; i++ ) + { + current_char = phrase[i]; + + /* makes sure the current character has no repetition */ + for (j = i+1; j < len_phrase; j++) + { + if (current_char == phrase[j]) + { + status = false; + + /* + because the given string is none isogram. + that means we can exit the nested for-loop. + */ + goto end; + } + } + } + + /* exit label */ + end: + return status; +} \ No newline at end of file diff --git a/exercism/isogram/isogram.h b/exercism/isogram/isogram.h new file mode 100644 index 00000000..ae28cfa6 --- /dev/null +++ b/exercism/isogram/isogram.h @@ -0,0 +1,8 @@ +#ifndef ISOGRAM_H +#define ISOGRAM_H + +#include + +bool is_isogram(const char phrase[]); + +#endif