Last active
December 10, 2015 02:08
-
-
Save rafkhan/4365016 to your computer and use it in GitHub Desktop.
This file contains 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> | |
#include <string.h> | |
void isort(int* arr, int len) { | |
int i, j; | |
for(i = 1; i < len; i++) { | |
int temp = arr[i]; | |
for(j = i; j > 0; j--) { | |
if(arr[j] < arr[j - 1]) { | |
int temp2 = arr[j]; | |
arr[j] = arr[j - 1]; | |
arr[j - 1] = temp2; | |
} else { | |
break; | |
} | |
} | |
arr[j] = temp; | |
} | |
} | |
int main(void) { | |
FILE *fp = fopen("example.txt", "r"); | |
int c, i, token_num = 0; | |
for(i = 0; (c = fgetc(fp)) != EOF; i++) {} //count number of chars in file | |
char *file_str = malloc(i * sizeof(char)); | |
for(fseek(fp, 0, SEEK_SET), i = 0; (c = fgetc(fp)) != EOF; i++) { file_str[i] = c; } | |
const char *delimeter = " `~!@#$%^&*()_-+={}[]<>:;\"'|\\,./?\n"; | |
char *temp_file_str = malloc(i * sizeof(char)); | |
memcpy(temp_file_str, file_str, i); | |
char *token = strtok(temp_file_str, delimeter); | |
if(token != NULL) { token_num++; } | |
while((token = strtok(NULL, delimeter)) != NULL) { token_num++; } | |
int *nums = malloc(token_num * sizeof(int)); | |
token = strtok(file_str, delimeter); | |
if((i = 0) && token != NULL) { nums[i] = atoi(token); i++; } | |
for(;(token = strtok(NULL, delimeter)) != NULL; i++) { nums[i] = atoi(token);} | |
isort(nums, token_num); | |
for(i = 0; i < token_num; i++) { printf("%d\n", nums[i]); } | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment