Last active
June 24, 2016 13:18
-
-
Save Highstaker/94f912df0945976bab1fc07f7b5489e3 to your computer and use it in GitHub Desktop.
C studies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#define MAX_HISTOGRAM_SYMBOLS 40 | |
int main(int argc, char const *argv[]) | |
{ | |
int ch; | |
unsigned int n_symbols[26]; | |
memset(n_symbols, 0, sizeof(int)*26); | |
unsigned int total_chars; | |
while((ch = getchar()) != '\n') | |
{ | |
if(ch >= 'A' && ch <= 'Z') | |
{ | |
n_symbols[ch - 'A']++; | |
} | |
else if(ch >= 'a' && ch <= 'z') | |
{ | |
n_symbols[ (ch^0b100000) - 'A']++; | |
} | |
total_chars++; | |
} | |
char i; | |
int j; | |
float perc; | |
int hist_symbols; | |
for(i=0;i<26;i++) | |
{ | |
if(n_symbols[i] !=0) | |
{ | |
putchar('A' + i); | |
perc = (float)n_symbols[i]/total_chars; | |
hist_symbols = perc * MAX_HISTOGRAM_SYMBOLS; | |
for(j = 0; j < MAX_HISTOGRAM_SYMBOLS; j++) | |
{ | |
if(j<hist_symbols) | |
{ | |
putchar('#'); | |
} | |
else | |
{ | |
putchar(' '); | |
} | |
} | |
printf("|\t%.1f\n", perc*100.0); | |
} | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
int main(int argc, char const *argv[]) | |
{ | |
int ndigits[10] = {0}; | |
memset(ndigits, 0, 10 * sizeof(int));//init with zeroes | |
int n_spaces = 0; | |
int n_others = 0; | |
int ch; | |
while((ch = getchar()) != '\n') | |
{ | |
if(ch >= '0' && ch <= '9') | |
{ | |
ndigits[ch - '0']++; | |
} | |
else if(ch == ' ') | |
{ | |
n_spaces++; | |
} | |
else | |
{ | |
n_others++; | |
} | |
} | |
int i; | |
for(i = 0; i<10; i++) | |
{ | |
printf("%d: %d\n", i, ndigits[i]); | |
} | |
printf("%s: %d\n%s: %d\n", "Spaces: ", n_spaces, "Others: ", n_others); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define STEP 20 | |
int main(int argc, char const *argv[]) | |
{ | |
if(argc < 4) | |
{ | |
printf("You didn't provide all the necessary parameters\n"); | |
return 1; | |
} | |
int start = atoi(argv[1]); | |
int end = atoi(argv[2]); | |
float celsius = 0; | |
printf("%s\t%s\n", "Fahrengheit", "Celsius"); | |
for(int i = start; i <= end; i=i+STEP) | |
{ | |
celsius = 5.0 * (i-32.0) / 9.0; | |
printf("%10d\t%6.1f\n", i, celsius); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(int argc, char const *argv[]) | |
{ | |
printf("Hallo, "); | |
printf("%s", "Welt!"); | |
printf("\n"); | |
printf("%d%s\n", argc, " arguments"); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(int argc, char const *argv[]) | |
{ | |
//counts symbols | |
unsigned int newlines, tabs, spaces; | |
newlines = tabs = spaces = 0; | |
int ch; | |
while(ch != 'a'){ | |
ch = getchar(); | |
if(ch == '\n') | |
{ | |
newlines++; | |
} | |
else if (ch == ' ') | |
{ | |
spaces++; | |
} | |
else if (ch == '\t') | |
{ | |
tabs++; | |
} | |
} | |
printf("%s: %d\n%s: %d\n%s: %d\n", "Newlines", newlines, "Tabs", tabs, "Spaces", spaces); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(int argc, char const *argv[]) | |
{ | |
printf("%d\n", EOF); | |
int ch; | |
// Assignment can be written together with expressions in condition check. | |
while( (ch = getchar()) != EOF) | |
{ | |
putchar(ch); | |
// ch ^= 0b100000; | |
// putchar(ch); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(int argc, char const *argv[]) | |
{ | |
//counts symbols | |
double n; | |
int ch; | |
while(ch != 'a'){ | |
ch = getchar(); | |
if(ch == '\n') | |
{ | |
n++; | |
} | |
} | |
printf("%.0f\n", n); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(int argc, char const *argv[]) | |
{ | |
//counts symbols | |
double nc; | |
for(nc = 0; getchar() != 'a'; ++nc) | |
; | |
printf("%.0f\n", nc); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(int argc, char const *argv[]) | |
{ | |
int ch = 0; | |
int prev_ch = 0; | |
while((ch = getchar()) != 'a') | |
{ | |
if(prev_ch == ' ' && ch == ' ') | |
; | |
else{ | |
putchar(ch); | |
} | |
prev_ch = ch; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(int argc, char const *argv[]) | |
{ | |
int ch; | |
while((ch = getchar()) != 'a') | |
{ | |
if(ch == '\b' || ch == '\t') | |
{ | |
putchar('\\'); | |
} | |
else | |
{ | |
putchar(ch); | |
} | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define OUT 0 | |
#define IN 1 | |
int main(int argc, char const *argv[]) | |
{ | |
unsigned long bytes, words, lines; | |
bytes = words = lines = 0; | |
int in_word = OUT; | |
int ch; | |
while( (ch = getchar()) != 'A' ) | |
{ | |
bytes++; | |
if(ch == '\n') | |
{ | |
lines++; | |
} | |
if(ch == ' ' || ch == '\n') | |
{ | |
if(in_word == IN) | |
{ | |
words++; | |
} | |
in_word = OUT; | |
} | |
else | |
{ | |
in_word = IN; | |
} | |
} | |
printf("%s: %lu\n%s: %lu\n%s: %lu\n", "Lines", lines, "Words", words, "Bytes", bytes ); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#define IN 1 | |
#define OUT 0 | |
#define MAX_LENGTH 200 | |
#define MAX_HISTOGRAM_SYMBOLS 40 | |
int main(int argc, char const *argv[]) | |
{ | |
int ch; | |
int in_word = OUT; //flag that shows if the caret is in word now | |
int lengths[MAX_LENGTH]; | |
memset(lengths, 0, sizeof(int) * MAX_LENGTH); | |
int wordlen = 0; | |
int total_words = 0; //amount of words | |
while((ch = getchar()) != '\n') | |
{ | |
if(ch == ' ' || ch == ',' || ch == '.') | |
{ | |
if(in_word == IN) | |
{ | |
in_word = OUT; | |
lengths[wordlen]++; | |
total_words++; | |
wordlen = 0; | |
} | |
} | |
else | |
{ | |
in_word = IN; | |
wordlen++; | |
} | |
} | |
//last word | |
if(in_word == IN) | |
{ | |
in_word = OUT; | |
lengths[wordlen]++; | |
total_words++; | |
wordlen = 0; | |
} | |
printf("Total %d\n",total_words); | |
//calculate percentages and draw histograms | |
int i, j; | |
float perc; | |
int hist_symbols_amount = 0; | |
for(i = 1; i < MAX_LENGTH; i++) { | |
if(lengths[i] != 0){ | |
perc = ((float)lengths[i])/total_words; | |
hist_symbols_amount = perc * MAX_HISTOGRAM_SYMBOLS; | |
printf("%3d: ", i); | |
for(j = 0; j < MAX_HISTOGRAM_SYMBOLS; j++) | |
{ | |
if(j < hist_symbols_amount){ | |
printf("#"); | |
} | |
else{ | |
printf(" "); | |
} | |
} | |
printf("|\t%.1f\n",perc*100); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment