mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-21 21:11:57 +03:00
Add files via upload
This commit is contained in:
parent
75f7866320
commit
c6cb7c8362
87
exercism/acronym/acronym.c
Normal file
87
exercism/acronym/acronym.c
Normal file
@ -0,0 +1,87 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
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;
|
||||
}
|
6
exercism/acronym/acronym.h
Normal file
6
exercism/acronym/acronym.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef ACRONYM_H
|
||||
#define ACRONYM_H
|
||||
|
||||
char *abbreviate(const char *phrase);
|
||||
|
||||
#endif
|
7
exercism/hello-world/hello_world.c
Normal file
7
exercism/hello-world/hello_world.c
Normal file
@ -0,0 +1,7 @@
|
||||
#include "hello_world.h"
|
||||
|
||||
const char *hello(void)
|
||||
{
|
||||
/* string is pointer of the first character */
|
||||
return "Hello, World!";
|
||||
}
|
6
exercism/hello-world/hello_world.h
Normal file
6
exercism/hello-world/hello_world.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef HELLO_WORLD_H
|
||||
#define HELLO_WORLD_H
|
||||
|
||||
const char *hello(void);
|
||||
|
||||
#endif
|
46
exercism/isogram/isogram.c
Normal file
46
exercism/isogram/isogram.c
Normal file
@ -0,0 +1,46 @@
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
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;
|
||||
}
|
8
exercism/isogram/isogram.h
Normal file
8
exercism/isogram/isogram.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef ISOGRAM_H
|
||||
#define ISOGRAM_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
bool is_isogram(const char phrase[]);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user