Skip to content
Snippets Groups Projects
Commit 690eec90 authored by Joey Le's avatar Joey Le
Browse files

Finished all of the TODOs

parent ab1fef64
No related branches found
No related tags found
No related merge requests found
......@@ -55,5 +55,6 @@
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
"C_Cpp_Runner.msvcSecureNoWarnings": false,
"C_Cpp.errorSquiggles": "disabled"
}
\ No newline at end of file
No preview for this file type
......@@ -16,9 +16,49 @@ int count_words(char *, int, int);
int setup_buff(char *buff, char *user_str, int len){
//First create a buffer and checks if the memory will be allocated properly
buff = malloc(BUFFER_SZ);
if (!buff) {
return -2;
}
//Variables to help move through the string and also count length
char *buff_ptr = buff;
char *str_ptr = user_str;
int white_space_checker = 0;
int length_of_string = 0;
while (*str_ptr != '\0') {
if (*str_ptr == ' ' || *str_ptr == '\t') {
if ( white_space_checker == 0 ) { //I know nested if's are bad but it's the onl way I could think of
*buff_ptr++ = ' '; // Using whitespace checker to determine to add it to the buffer or not
length_of_string++;
white_space_checker = 1;
}
} else {
*buff_ptr++ = *str_ptr;
length_of_string++;
white_space_checker = 0;
}
if (length_of_string >= BUFFER_SZ) { //Checks if the length is past the buffer or not
free(buff);
return -1;
return 0; //for now just so the code compiles.
}
str_ptr++;
}
while (length_of_string < BUFFER_SZ) { //To fill up buffer with .
*buff_ptr++ = '.';
length_of_string++;
}
return length_of_string;
}
void print_buff(char *buff, int len){
......@@ -35,9 +75,79 @@ void usage(char *exename){
}
int count_words(char *buff, int len, int str_len) {
//YOU MUST IMPLEMENT
return 0;
if (buff == NULL || str_len <= 0) { //Makes sure it valid
printf("Error: Invalid input.\n");
return -1;
}
char *buff_ptr = buff;
int words_in_string = 0;
while (*buff_ptr != '\0') {
if (*buff_ptr == ' ' ) { //I assume that space is a new word so it checks for that everytime
words_in_string++ ; //If it's not space it keeps reading
}
buff_ptr++;
printf("Word Count %d\n", words_in_string);
return words_in_string;
}
}
int reverse_words(char *buff, int len, int str_len) {
if (buff == NULL || str_len <= 0 ) {
printf("Error: Invalid input.\n") ;
return -1 ;
}
char temp;
int start = 0 ;
int end = str_len - 1;
while (start < end) { //Its like a circle that flips the word around
temp = buff[start]; //My first idea was to make a another array, but that would have took to much time
//Then I remebered about temp varaibles
buff[start] = buff[end];
buff[end] = temp;
start++;
end--;
}
buff[str_len] = '\0'; //Ends the string so the buffer thingy is not printed
printf("Reversed String: &s\n ", buff);
}
int write_string(char* buff, int len, int str_len) {
printf("Word Print\n----------\n");
char*buff_ptr = buff;
char word[BUFFER_SZ];
int word_count = 0;
int line_number = 1;
int line_position = 0;
//I use a loop to print out each line one at a time
while (*buff_ptr != '\0') {
if (*buff_ptr != ' ') {
word[line_position] = buff_ptr[line_position];
word_count++;
} else {
word[line_position] = '\0'; //Resets the word
printf("%i. %s (%i)", line_number,word,word_count);
line_number++;
line_position = 0;
word_count = 0 ;
//reset word counter
}
*buff_ptr++;
}
}
//ADD OTHER HELPER FUNCTIONS HERE FOR OTHER REQUIRED PROGRAM OPTIONS
......@@ -51,6 +161,13 @@ int main(int argc, char *argv[]){
//TODO: #1. WHY IS THIS SAFE, aka what if arv[1] does not exist?
// PLACE A COMMENT BLOCK HERE EXPLAINING
// argv is a way to split a string up into seperate pieces
//The purpose of argv[0] is that it allows you to call a certain part of the command line using
//A bracket and number
//This is a way to be able to pass on arguements into the function
// and it's make the program a lot easier to work with for developers and users alike
if ((argc < 2) || (*argv[1] != '-')){
usage(argv[0]);
exit(1);
......@@ -67,7 +184,8 @@ int main(int argc, char *argv[]){
//WE NOW WILL HANDLE THE REQUIRED OPERATIONS
//TODO: #2 Document the purpose of the if statement below
// PLACE A COMMENT BLOCK HERE EXPLAINING
// The purpose of this is that it checks how many arguements are passed into it to make sure that
// It has the arguements required to function properly. If it's less than 3 then it exits the program
if (argc < 3){
usage(argv[0]);
exit(1);
......@@ -80,6 +198,10 @@ int main(int argc, char *argv[]){
// return code of 99
// CODE GOES HERE FOR #3
buff = (char *)malloc(BUFFER_SZ * sizeof(char));
if (buff == NULL) {
printf("Error: Memory buggin fwah ");
}
user_str_len = setup_buff(buff, input_string, BUFFER_SZ); //see todos
if (user_str_len < 0){
......@@ -99,15 +221,31 @@ int main(int argc, char *argv[]){
//TODO: #5 Implement the other cases for 'r' and 'w' by extending
// the case statement options
case 'r':
rc = reverse_words(buff, BUFFER_SZ, user_str_len);
if (rc < 0) {
printf("Error reversing words, rc = $dn\n",rc);
exit(2);
}
case 'w':
rc = write_string(buff, BUFFER_SZ, user_str_len);
if (rc < 0) {
printf("Error writing string, rc = %d\n", rc);
exit(2);
default:
usage(argv[0]);
exit(1);
break;
}
//TODO: #6 Dont forget to free your buffer before exiting
print_buff(buff,BUFFER_SZ);
free(buff);
exit(0);
}
}
//TODO: #7 Notice all of the helper functions provided in the
// starter take both the buffer as well as the length. Why
......@@ -115,4 +253,11 @@ int main(int argc, char *argv[]){
// is a good practice, after all we know from main() that
// the buff variable will have exactly 50 bytes?
//
// PLACE YOUR ANSWER HERE
\ No newline at end of file
// I think it's good practice, because having both the pointer
// passing the buffer size everything is important, because
// then it makes the code more readable and also it can make it
// so that it will be a bit easier to reused in the future
// because you can put in different buffers and stuff and
// you can prevent a lot of errors, because you can check the size
// of the buffer whenever.
//
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment