Part C and Revisions

This commit is contained in:
2023-04-09 23:44:45 +00:00
parent 1a9dfbc32a
commit 3dcfa29624
6 changed files with 67 additions and 6 deletions
+16
View File
@@ -0,0 +1,16 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "c++98",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
-1
View File
@@ -9,7 +9,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Globals to Simplify Passing Around
// Contains pointer to queue memory
-5
View File
@@ -9,7 +9,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int ptr_size = 0;
char* ptr;
@@ -31,10 +30,6 @@ char pop() {
void print() {
for (int i = ptr_size - 1; i >= 0; i--)
printf("%c\n",*(ptr+i));
/*
for (int i = 0; i < ptr_size; i++)
printf((i + 1 != ptr_size)?"%c, ":"%c\n",*(ptr+i));
*/
}
int main() {
+27
View File
@@ -0,0 +1,27 @@
/*
COS135 - HW9c
Nicholas Pease
Reverse String
*/
#include <stdio.h>
#include <string.h>
#include "PEASE.h"
int main() {
printf("Reverse a phrase: ");
char input[20];
fgets(input,20,stdin);
for (int i=0;i<strlen(input);i++) if (input[i] == '\n') input[i] = '\0';
char reverse[strlen(input)];
for (int i = 0; i < strlen(input); i++) {
push(input[i]);
}
for (int i = 0; i < strlen(input); i++) {
reverse[i] = pop();
}
printf("Reversed output is \"%s\"\n",reverse);
return 0;
}
+24
View File
@@ -0,0 +1,24 @@
// Library File for Part C adapting my code from part B
#ifndef PEASE_H_
#define PEASE_H_
#include <stdlib.h>
int ptr_size = 0;
char* ptr;
void push(char addition) {
// Deal with allocation every time push is called and pop. If not initialized, the source is NULL
ptr = realloc((ptr_size == 0)? NULL: ptr, (ptr_size+1)*sizeof(char));
*(ptr+ptr_size) = addition;
ptr_size++;
}
char pop() {
char removal = *(ptr+ptr_size-1);
ptr_size--;
ptr = realloc((ptr_size == 0)? NULL: ptr, (ptr_size)*sizeof(char));
return removal;
}
#endif
BIN
View File
Binary file not shown.