This will be where we store the work we've achieved in Discrete. Hey all, The following code is what I have for my functions so far. If anybody has written a more efficient way of doing any of the functions here please feel free to replace them. -Dave Schoeffler These all look great, Dave. I'm not seeing any improvements I, personally, can make. We can use these as our library, unless anyone has any conflicts or improvements with any of the functions. Were we supposed to have a delete char function? Do not forget you can call previously written functions from new ones. You can call the counter function from append. Yes. There are many functions that were assigned and not everyone was to write every single one. So if you have a function currently not listed here please go ahead and add it. -Dave Schoeffler ===== String Functions ===== ====Count chars in a string==== //Function count() receives a string of characters as input and returns //the number of character as an integer int count(char *string) { int letterCount = 0; while(string[letterCount] != '\0') { letterCount++; } return letterCount; } ====Count specific chars in a string==== //Function countC() receives a string of characters and a single character //as input and returns the number of times the given character appeared //in the string as an integer int countC(char *string, char ch) { int letterCount = 0,i = 0; while(string[i] != '\0') { if(string[i] == ch) { letterCount++; } i++; } return letterCount; } ====append a character to the end==== //Function append() receives as input a string and a character and //returns a new string that has that has the character appended on the end char* append(char *string, char ch) { int i = 0,j = 0; while(string[j] != '\0') { j++; } char *newString; newString = malloc(sizeof(char)*(j+2)); while(string[i] != '\0') { newString[i] = string[i]; i++; } newString[i] = ch; newString[i + 1] = '\0'; return newString; } ====prepend a character to the front==== //Function prepend() receives as input a string and a character and //returns a new string that has that has the character prepended to the beginning char* prepend(char *string, char ch) { int i = 0,j = 0, k = 0; while(string[j] != '\0') { j++; } char *newString; newString = malloc(sizeof(char)*(j+2)); newString[i] = ch; i+=1; while(string[k] != '\0') { newString[i] = string[k]; i++; k++; } newString[i] = '\0'; return newString; } ====find a character==== //Function find() receives as input a string and a character and returns //the position that the given character appears in the string. If the //character does not appear in the string the function returns a -1. int find(char *string, char ch) { int FLAG = 0; int position,i = 0; while(string[i] != '\0' && FLAG == 0) { if(string[i] == ch) { position = i + 1; FLAG = 1; } i++; } if(FLAG == 0) { position = -1; } return position; } ====Concatenate two strings==== //Function concat() receives as input two strings and and retrurns a //concatenated string. char* concat(char *string1, char *string2) { int i = 0,j = 0,k = 0; while(string1[j] != '\0') { j++; } while(string2[k] != '\0') { k++; } char *newString; newString = malloc(sizeof(char)*(j+k+1)); k = 0; while(string1[i] != '\0') { newString[i] = string1[i]; i++; } while(string2[k] != '\0') { newString[i] = string2[k]; i++; k++; } newString[i] = '\0'; return newString; } ====insert a character==== //Function insert() receives as input a string, a character and an integer. //It then inserts the given character to the given position and returns a new //string. char* insert(char* string,char ch, int position) { int i = 0,j = 0, k = 0; while(string[j] != '\0') { j++; } char *newString; newString = malloc(sizeof(char)*(j+2)); if(position > (j+1)) { position = j+1; } for(i = 0;i < position - 1; i++) { newString[i] = string[k]; k++; } newString[i] = ch; i++; while( string[k] != '\0') { newString[i]= string[k]; i++; k++; } newString[j+2] = '\0'; return newString; } ====to all lower==== //Function toLower() receives as input a string and then coverts // the given string to all lowercase and then returns the new string. char* toLower(char* string) { int i = 0, j = 0; int test; while(string[j] != '\0') { j++; } for(i = 0; i < j; i++) { test = (int)(string[i]); if(test > 64 && test < 91) { test = test + 32; (char)(test); string[i] = test; } } return string; } ====all to upper==== //Function toUpper() receives as input a string and then coverts // the given string to all uppercase and then returns the new string. char* toUpper(char* string) { int i = 0, j = 0; int test; while(string[j] != '\0') { j++; } for(i = 0; i < j; i++) { test = (int)(string[i]); if(test > 96 && test < 123) { test = test - 32; (char)(test); string[i] = test; } } return string; } ====invert case==== //Function invertCase() receives as input a string and //returns a string that has each of its characters //inverted in case char* invertCase(char* string) { int i = 0, j = 0; int test; while(string[j] != '\0') { j++; } for(i = 0; i < j; i++) { test = (int)(string[i]); if(test > 64 && test < 91) { test = test + 32; (char)(test); string[i] = test; } else if ( test > 96 && test < 123) { test = test - 32; (char)(test); string[i] = test; } } return string; } ---- ====String difference==== Sdifference is a function that finds the difference of two strings and returns the characters that are in the first string and not the second. accepts: Two pointers, each to a character array returns: a pointer to a character array char* Sdifference(char *one, char *two) { int i = 0, j = 0; while(one[i] != '\0') { if(one[i] == two[j]) one = removeachar(one, i); else j++; if(two[j] == '\0') { j = 0; i++; } } i=0; while(one[i] != '\0') { if(one[i] == one[i+1]) one = removeachar(one, i+1); else i++; } return one; } ====String intersect==== Sintersect is a function that finds where two strings are alike and returns those characters. accepts: two pointers, both to character arrays returns: a pointer to a character array char* Sintersect(char *one, char *two) { int i = 0, j = 0, k = 0; char* gamma; gamma = malloc (sizeof(char)*(60)); while((one[i] != '\0') && (two[j] != '\0')) { if(one[i] == two[j]) { gamma[k] = two[j]; k++; } j++; if(two[j] == '\0') { j = 0; i++; } } return gamma; } ====Header file==== //Header file for mystrings.c int find(char *, char ); int count(char *); int countC(char *, char ); char* append(char *, char ); char* prepend(char *, char ); char* insert(char *,char , int); char* concat(char *, char *); char* toLower(char*); char* toUpper(char*); char* invertCase(char*); char* Sintersect(char *, char *); char* Sdifference(char *, char *); Here's an untested delete function: Header: char* deleteChar(char*, char); Code: char* deleteChar(char* str1, char c) { int instances = countChar(str1, c); char *newString = malloc(sizeof(char) * count(str1) - (instances - 1)); while(1) { bool endOfString = false; while(endOfString == false) { if(*str1 != c) { *str1 = *str1; str1++; } if(*str1 == c) str1++; if(*str1 == '\0') endOfString = true; } return(newString); } ===Documentation=== Here is the documentation so far. If you add a function to this page make sure you add to the documentation as well. Thanks. -Dave Schoeffler //This file contains the documentation for the use of the function //contained in mystrings.c // //The following are the function prototypes describing the parameters //and return type of each of the functions // /* int count(char *); * int countC(char *, char ); * char* append(char *, char ); * char* prepend(char *, char ); * char* insert(char *,char , int); * char* concat(char *, char *); * int find(char *, char ); * char* toLower(char*); * char* toUpper(char*); * char* invertCase(char* ); */ //int count(char *); //Function count() receives a string of characters as input and returns //the number of character as an integer. //int countC(char *, char ); //Function countC() receives a string of characters and a single character //as input and returns the number of times the given character appeared //in the string as an integer //char* append(char *, char ); //Function append() receives as input a string and a character and //returns a new string that has that has the character appended on the end //char* prepend(char *, char ); //Function prepend() receives as input a string and a character and //returns a new string that has that has the character prepended to the beginning //char* insert(char *,char , int); //Function insert() receives as input a string, a character and an integer. //It then inserts the given character to the given position and returns a new //string. //char* concat(char *, char *); //Function concat() receives as input two strings and and retrurns a //concatenated string. //int find(char *, char ); //Function find() receives as input a string and a character and returns //the position that the given character appears in the string. If the //character does not appear in the string the function returns a -1. //char* toLower(char*) //Function toLower() receives as input a string and then coverts // the given string to all lowercase and then returns the new string. //char* toUpper(char*); //Function toUpper() receives as input a string and then coverts // the given string to all uppercase and then returns the new string. //Function invertCase() receives as input a string and //returns a string that has each of its characters //inverted in case ===Individual programming assignments=== Hey all here are the specific programs that we each decided to write. === Jeff === http://lab46.corning-cc.edu/~jjohns43/deleteChar.c return the number of common characters from two strings. Prototype:int commonNum(char* , char* );//example string1 = "abcd", string2 = "cdef". The function should return an int 2 ===Cory=== replace first character of string. Prototype: char* replaceFirst(char* ); replace last character of string. Prototype: char* replaceLast(char* ); replace any character of string. Prototype: char* replace(char* ); === Dave === Once you have a function written and added to the functions file remove it from the above list. Thanks. If any of you have more functions that we haven't mentioned yet add them below and someone can add them to there own assignments. Dave Schoeffler ---- =====Set Functions===== This will be the collaboration station for our set functions in discrete. To remind everyone, our goal by the end of the course is to create functions to manipulate sets and implement them into a card game, specifically texas hold'em. ====Check set notation==== //checkSetNotation.c //John T. Rine //October 15, 2011 #include"set.h" #include int checkSetNotation(char *inputString) { char oldChar = 'C'; int position = 0; int numberOfCharsInSet = 0; numberOfCharsInSet = cntChars(inputString); if(*(inputString + position) != '{') { printf("First character in a set representation must be a '{'\n"); return 0; } else if (*(inputString + numberOfCharsInSet - 1) != '}') { printf("Last character in a set representation must be a '}'\n"); return 0; } else if (*(inputString + numberOfCharsInSet - 2) == ',') { printf("Last two characters in a set representation can't be \",}\"\n"); return 0; } else if (*(inputString + 1) == ',') { printf("First two characters in a set representation can't be \"{,\"\n"); return 0; } while(*(inputString + position) != '\0') { if (*(inputString + position) == ',' && oldChar == ',') { printf("Can't have two adjacent commas in a set representation\n"); return 0; } oldChar = *(inputString + position); position ++; } return 1; } ====CntChars==== //cntChars.c //John T. Rine //September 23, 2011 #include"set.h" int cntChars(char *inputString) { int count = 0; while(*(inputString + count) != '\0') ++count; return(count); } ====Displayset==== //displaySet.c //John T. Rine //October 15, 2011 #include"set.h" #include void displaySet(set inputSet) { int i; printf("{"); for(i = 0; i < inputSet.numberOfElements; i++) { printf("%s", inputSet.setPtr[i]); if(i != inputSet.numberOfElements - 1) printf(","); } printf("}\n"); } ====occurrences==== //occurrences.c //John T. Rine //September 23, 2011 #include "set.h" int occurrences(char *inputString, char c) { int i = 0; int r = 0; while (*(inputString + i) != '\0') { if(*(inputString + i) == c) r++; i++; } return r; } ====parseset==== //parseSet.c //John T. Rine //October 15, 2011 #include "set.h" #include set parseSet(char *setInput) { int i, count = 0; char **rowPtr = NULL; int stringCharacterPosition = 0; int elementNumber = 0; int elementCount = 0; int elementCharacterCount = 0; int oldElementCharacterCount = 0; int oldStringCharacterPosition = 0; set returnSet; if(checkSetNotation(setInput) == 0) exit(1); elementCount = occurrences(setInput, ',') + 1; rowPtr = malloc(sizeof(char *) * elementCount); while(*(setInput + stringCharacterPosition) != '\0') { if(*(setInput + stringCharacterPosition) == '{') stringCharacterPosition++; oldElementCharacterCount = elementCharacterCount; oldStringCharacterPosition = stringCharacterPosition; while(*(setInput + stringCharacterPosition) != ',' && *(setInput + stringCharacterPosition) != '}') { elementCharacterCount++; stringCharacterPosition++; } *(rowPtr + elementNumber) = malloc(sizeof(char) * (elementCharacterCount + 1)); count = 0; while(*(setInput + oldStringCharacterPosition) != ',' && *(setInput + oldStringCharacterPosition) != '}') { *(*(rowPtr + elementNumber) + count) = *(setInput + oldStringCharacterPosition); oldStringCharacterPosition++; count++; } *(*(rowPtr + elementNumber) + count) = '\0'; elementCharacterCount = 0; elementNumber++; stringCharacterPosition++; } returnSet.setPtr = rowPtr; returnSet.numberOfElements = elementNumber; return returnSet; } ====settest==== //setTest.c //John T. Rine //October 16, 2011 #include #include #include"set.h" int main(int argc, char **argv) { set setData; int i; if (argc == 1) { printf("Should be at least one command line argument supplied after the file name\n"); exit(1); } else setData = parseSet(*(argv + 1)); displaySet(setData); for (i = 0; i < setData.numberOfElements; i++) free(setData.setPtr[i]); free(setData.setPtr); return 0; } ====set.h==== //set.h //John T. Rine //October 15, 2011 #ifndef _SET_H #define _SET_H struct Set { char ** setPtr; int numberOfElements; }; typedef struct Set set; set parseSet(char *); int cntChars(char *); int checkSetNotation(char *); int occurrences(char *, char); void displaySet(set); #endif