COMPUTER SCIENCE: POINTERS

profilekritha
templates-v6.zip

PA202/main.c

// TODO - Nothing! Do not modify this file #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include "tools.h" extern void runAllTests(); int main() { printf("Welcome to PA202\n\n"); runAllTests(); return EXIT_SUCCESS; }

PA202/PA202.cbp

PA202/PA202.depend

# depslib dependency file v1.0
1318368000 source:c:\users\alessio\desktop\workbench\todos\pa202\pa202\tests.c
<stdlib.h>
1318386310 source:c:\users\alessio\desktop\workbench\todos\pa202\pa202\tools.c
<stdlib.h>
<ctype.h>
1318364400 source:c:\users\alessio\desktop\workbench\todos\edu - [3515]\w09 - 202 -\students files\pa202\tests.c
<stdlib.h>
1320062645 source:c:\users\alessio\desktop\workbench\todos\edu - [3515]\w09 - 202 -\students files\pa202\tools.c
<stdlib.h>
<ctype.h>
1408419945 source:c:\users\alessiolpt\desktop\workbench\202\pa202\main.c
<stdio.h>
<stdlib.h>
<string.h>
tools.h
1408419790 c:\users\alessiolpt\desktop\workbench\202\pa202\tools.h
1408420799 source:c:\users\alessiolpt\desktop\workbench\202\pa202\tests.c
<stdlib.h>
<stdio.h>
<string.h>
testlib.h
tools.h
1408406793 c:\users\alessiolpt\desktop\workbench\202\pa202\testlib.h
1408419752 source:c:\users\alessiolpt\desktop\workbench\202\pa202\tools.c
<stdlib.h>
<ctype.h>
1408406802 source:c:\users\alessiolpt\desktop\workbench\202\pa202\testlib.c
<stdlib.h>
<stdio.h>
testlib.h
1408419945 source:c:\users\alessiolpt\desktop\workbench\ongoing stuff\202\pa202\main.c
<stdio.h>
<stdlib.h>
<string.h>
tools.h
1408419790 c:\users\alessiolpt\desktop\workbench\ongoing stuff\202\pa202\tools.h
1408419752 source:c:\users\alessiolpt\desktop\workbench\ongoing stuff\202\pa202\tools.c
<stdlib.h>
<ctype.h>
1408514766 source:c:\users\alessiolpt\desktop\workbench\ongoing stuff\202\pa202\testlib.c
<stdlib.h>
<stdio.h>
testlib.h
1408514693 c:\users\alessiolpt\desktop\workbench\ongoing stuff\202\pa202\testlib.h

PA202/PA202.layout

PA202/testlib.c

// TODO - nothing, do not modify this file #include <stdlib.h> #include <stdio.h> #include "testlib.h" void TEST(const char * const shortDescription, int boolexp){ // keep track of tests numbers static int testNumber = 0; testNumber++; // string representing the outcome of the test char* testResult = NULL; // determining the outcome if(boolexp){ testResult = "OK"; }else{ testResult = "FAILS"; } printf("[Test] #%3d --> %-5.5s\t%-50.50s\n", testNumber, testResult, shortDescription); fflush(stdout); }

PA202/testlib.h

// TODO - nothing, do not modify this file #ifndef TESTLIB_H_INCLUDED #define TESTLIB_H_INCLUDED void TEST(const char * const shortDescription, int boolexp); /* ROLE This function display information about the test being "OK" or "FAILED" based on whether the condition used as parameter is respectively true or false PARAMETERS shortDescription a string describing the test. 45 characters long maximum boolexp the result of evaluating a boolean expression representing what we are testing. If it evaluates to true, the function will display that the test succeeded. Otherwise, it will display that the test failed. */ #endif // TESTLIB_H_INCLUDED

PA202/tests.c

// TODO - Implement your tests in this file using testlib functions #include <stdlib.h> #include <stdio.h> #include <string.h> #include "testlib.h" #include "tools.h" // Number of tests in the arrays below const int NB_TESTS = 3; // strings to be used with words_modify when testing const char *tests_inputs[] = { "hello", /* making sure we translate a single word */ "how are you", /* same with multiple words */ "several spaces" /* several spaces in a row are just fine */ }; // Expected results from words_modify when applied to the above const char *tests_expected[] = { "olleh", "woh era uoy", "lareves secaps" }; void runAllTests(){ /* ROLE applies all the tests to our functions from tools.c */ int i; char *test_input = NULL; /* what we provide our function with */ const char *test_expected = NULL; /* what we expect the result to be */ char *test_observed = NULL; /* what our function returns */ int test_outcome; /* test successful or not? */ char buffer[2048]; // Iterating over all available tests for(i=0; i < NB_TESTS; i++){ // setting up this specific test test_input = strdup(tests_inputs[i]); test_expected = tests_expected[i]; // getting the result from our implementations words_modify(test_input); // test_input has been modified test_observed = test_input; // is the outcome what we expected? test_outcome = (test_observed? strcmp(test_observed, test_expected) : test_observed == test_expected); // working around strcmp policy to not avoid NULL pointers sprintf(buffer, "%-30.30s", tests_inputs[i]); // verifying if observed result is the expected one */ TEST(buffer, test_outcome == 0); // de-allocating memory allocated by words_modify if (test_observed){ free(test_observed); } test_input = NULL; // test_input is now a dangling pointer } }

PA202/tools.c

// TODO - Implement the functions in this file #include <stdlib.h> #include <ctype.h> void word_reverse( char* str ){ /* ROLE Reverses the order of the character in the string. Does not modify the end of string marker '\0'. PARAMETERS str - the string to reverse */ // THE CODE INSIDE IS ONLY A TEMPLATE - REPLACE IT ALL! while(*str){ *str = '*'; str++; } } int is_separator( char data ){ /* ROLE Tests if the character passed as parameter is a separator; i.e. something different than a letter or digit. RETURNS 1 - if data is a separator 0 - otherwise PARAMETERS data - the character to test */ return ( !isalnum(data) ); } int words_initialize( char* str, char* words[], int maxwords ){ /* ROLE Parses the string str and identify each word in it. Words are separated by any character which makes the is_separator function returns true. The address of the first character of each word in the string str, is stored as a pointer in the array words. When we find the end of the word we just added in the string str, we modify it so that the next character becomes '\0'. We are not able to handle more than maxwords words and subsequent ones will be ignored. Our function will return the number of words it found which also indicates how many of the maxwords elements in the array words are meaningful. RETURNS the number of words identified in the string input. If any parameters are inappropriate, return 0 PARAMETERS input - string in which we will identify words words - an array of pointer on strings which will be the words we have identified maxwords - the size of the words array of pointers */ words[0] = str; return(1); } void word_handle_marker(char* str){ // WORKING FUNCTION - JUST DOCUMENT :) - GIFT /* ROLE Replaces the end of string marker '\0' by a space ' ' PARAMETERS str - the string to modify */ if(!str) return; // handling parameter being a NULL string while(*str){ str++; } *str = ' '; } void words_modify(char* str){ const int MAXWORDS = 256; char* allwords[MAXWORDS]; int i; int nbwords; if(!str) return; // handling parameter being a NULL string /* initialize all pointers in allwords to NULL */ for(i=0; i < MAXWORDS ; i++) allwords[i] = NULL; nbwords = words_initialize(str, allwords, MAXWORDS); for(i=0 ; i < nbwords ; i++){ word_reverse(allwords[i]); } for(i=0 ; i < (nbwords-1) ; i++){ word_handle_marker(allwords[i]); } }

PA202/tools.h

// TODO - nothing, do not modify this file #ifndef _TOOLS_H_ #define _TOOLS_H_ void words_modify( char* str ); #endif