Compare commits

..

No commits in common. "bb0a1a5eef878f5140ed7eca041ee9e6de7f97f9" and "4c9614342e0cc6e509324f21b5c2a097e991442a" have entirely different histories.

3 changed files with 29 additions and 51 deletions

View File

@ -13,6 +13,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main() int main()
@ -26,14 +28,12 @@ int main()
char buf[4]; char buf[4];
fgets(buf, 3, stdin); fgets(buf, 3, stdin);
int choice = strtol(buf, NULL, 10); int choice = strtol(buf, NULL, 10);
//char* msg = calloc(128, sizeof(char));
fputs("Enter your message text: ", stdout); fputs("Enter your message text: ", stdout);
//if (msg) fgets(msg, 128, stdin);
char* msg = dyninput_str(INT64_MAX); char* msg = dyninput_str(INT64_MAX);
if (!msg)
{
puts("ERROR: String input failed! Exiting...");
return -1;
}
fputs("Enter your key (shift value): ", stdout); fputs("Enter your key (shift value): ", stdout);
fgets(buf, 4, stdin); fgets(buf, 4, stdin);

View File

@ -12,12 +12,6 @@
#include <stdbool.h> #include <stdbool.h>
/*
* For built in C mod operator (%) -1 % 26 == -1
* For the requirements of this algorithm -1 mod 26 == 25 is required
* The below functions accomplish this
*/
// Which of the two below functions is faster (find out!) // Which of the two below functions is faster (find out!)
inline int mod(int a, int b) inline int mod(int a, int b)
{ {
@ -32,11 +26,6 @@ inline int fast_mod(int a, int b)
} }
/*
* 65 - 90 = ASCII 'A' - 'Z'
* 97 - 122 = ASCII 'a' - 'z'
*/
inline bool isupper(char c) inline bool isupper(char c)
{ {
return c >= 'A' && c <= 'Z'; return c >= 'A' && c <= 'Z';

View File

@ -15,44 +15,33 @@
char* dyninput_str(int_least64_t maxsize) char* dyninput_str(int_least64_t maxsize)
{ {
char* str = malloc(3 * sizeof(char)); char* str = malloc(3 * sizeof(char));
if (!str) /*if (!str)
{ puts("Error allocating memory!");
puts("\nError allocating memory!"); return NULL;*/
return NULL;
}
int_least64_t size = 3, i = 0; int_least64_t size = 3, i = 0;
char c = 0; char c = 0;
do do
{ {
if (size == maxsize) c = getchar();
{ if (i + 2 == size)
printf("Character limit of %lld reached!\n", maxsize); {
break;
}
c = (char)getchar();
if (i + 1 == size)
{
char* tmp = realloc(str, size + 1); char* tmp = realloc(str, size + 1);
if (!tmp) if (!tmp)
{ {
puts("Error (re)allocating memory!"); puts("Error (re)allocating memory!");
return NULL; return NULL;
} }
str = tmp; str = tmp;
size++; size++;
} }
str[i] = c; str[i] = c;
i++; i++;
} while (c && c != '\n'); } while (c && c != '\n');
if (str[i - 1] != '\n') str[i - 1] = '\n'; str[i] = '\0';
str[i] = '\0';
// Sanitise stdin return str;
while (c != '\n') c = (char)getchar();
return str;
} }