Added other excercises

This commit is contained in:
2016-03-16 14:00:24 +01:00
parent b19dac39e8
commit eeb80408fd
2 changed files with 231 additions and 0 deletions
+223
View File
@@ -19,6 +19,7 @@ void main()
printf("Nieuw string: %s\n", s1);*/
//gaat goed
/*
char s1[10];
strcpy(s1, "taart");
@@ -44,7 +45,29 @@ void main()
strcpy(s6, "verjaardagstaart");
printf("Result taart en verjaardagstaart: %d\n", strend(s5, s6));
*/
//fahrenheit();
//celsius();
//char str[128];
//strcpy(str, "Hallo dit is een zin die best wel heel erg lang zou kunnen zijn voor c programming");
//reverse_string(str);
//char str[32];
//strcpy(str, "Test f ");
//printf("%s:\n", str);
//trim(str);
//printf("%s:\n", str);
char str1[32];
char str2[16];
strcpy(str1, "acccddddefghijk");
strcpy(str2, "bdcgij");
//remove_chars(str1, str2);
printf("Index of first same char: %d\n",first(str1, str2));
}
void strCat(char* a, char* b)
@@ -81,4 +104,204 @@ int strend(char s[], char t[])
}
return 1;
}
void fahrenheit(void)
{
float fahr, celsius;
int laagste, hoogste, interval;
laagste = 0;
hoogste = 300;
interval = 20;
fahr = laagste;
printf("\n F C\n");
printf("-----------\n");
while (fahr <= hoogste)
{
celsius = (5.0 / 9.0) * (fahr - 32.0);
printf("%3.0f %6.1f\n", fahr, celsius);
fahr = fahr + interval;
}
}
void celsius(void)
{
float fahr, celsius;
int laagste, hoogste, interval;
laagste = 0;
hoogste = 150;
interval = 10;
celsius = laagste;
printf("\n C F\n");
printf("-----------\n");
while (celsius <= hoogste)
{
fahr = (celsius*1.8) + 32.0;
printf("%3.0f %6.1f\n", celsius, fahr);
celsius = celsius + interval;
}
}
void histogram_length(char * string)
{
int length = 0;
strcat(string, " ");
printf("%s\n", string);
for (; *string; string++)
{
if (*string != ' ' && *string != '\0')
{
length++;
}
else
{
for (int i = 0; i < length; i++)
printf("-");
printf("\n");
length = 0;
}
}
}
void histogram_freq(char * string)
{
int chfreq[128] = {0};
printf("%s\n", string);
for (; *string; string++)
{
if (*string == ' ')
continue;
int chid = *string - '0';
chfreq[chid]++;
}
for (int i = 0; i < 128; i++)
{
if (chfreq[i] != 0)
{
printf(" %c ", i + '0');
for (int p = 0; p < chfreq[i]; p++)
printf("-");
printf("\n");
}
}
}
void reverse_string(char * string)
{
int chars = strlen(string);
char *buffer = malloc(sizeof(char) * chars);
int t = chars - 1;
for (int i = t; i >= 0; i--)
{
buffer[chars - 1 - i] = string[i];
}
strncpy(string, buffer, chars);
printf("%s\n", string);
free(buffer);
}
void trim(char *str)
{
char *end;
if (*str == 0) // All spaces?
return;
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && isspace(*end)) end--;
// Write new null terminator
*(end + 1) = '\0';
str = end;
}
void remove_chars(char * str1, char * str2)
{
int chex[128] = { 0 };
for (; *str2; str2++)
{
if (*str2 == ' ')
continue;
int chid = *str2 - '0';
chex[chid] = 1;
}
int chars = strlen(str1);
int count = 0;
char *buffer = malloc(sizeof(char) * chars);
for (; *str1; str1++)
{
if (chex[*str1 - '0'] != 1)
{
printf("Char %c stays\n", *str1);
buffer[count] = *str1;
count++;
}
else
printf("Char %c removed\n", *str1);
}
str1[count] = '\0';
strncpy(str1, buffer, count);
printf("%s\n", str1);
free(buffer);
}
int first(char * str1, char * str2)
{
int chex[128] = { 0 };
for (; *str2; str2++)
{
if (*str2 == ' ')
continue;
int chid = *str2 - '0';
chex[chid] = 1;
}
int count = 0;
for (; *str1; str1++)
{
if (chex[*str1 - '0'] == 1)
{
return count;
}
count++;
}
return -1;
}
+8
View File
@@ -1,3 +1,11 @@
#pragma once
void fahrenheit(void);
void celsius(void);
void histogram_length(char *);
void histogram_freq(char *);
void reverse_string(char *);
void trim(char *);
void remove_chars(char *, char *);
int first(char *, char *);
void strCat(char* a, char* b);
int strend(char s[],char t[]);