John T. Rine
Discrete Structures
Fall 2011
My library is available at:
http://lab46.corning-cc.edu/~jr018429/Library.zip
The Discrete group to which I belong has a Wiki page is located at:
http://lab46.corning-cc.edu/notes/discrete/setman
My library is composed of the following functions:
int cntChars(char *)
The function cntChars takes as its parameter a character pointer (the base address of a character array, a string, and returns the number of characters in the string up to the maximum size of an integer).
Example code:
//John T. Rine //September 23, 2011 #include<stdio.h> #include "jr.h" int main(int argc, char **argv) { char myString[]="John Rine"; printf("%s\n", myString); printf("The number of character in my string is %d\n", cntChars(myString)); return 0; }
Output:
lab46:~/Library$ ./cntChars Please enter a string between 1 and 250 characters long John Rine The number of capital characters in your string is: 2 The number of lower case characters in your string is: 6 The number of number characters in your string is: 2 lab46:~/Library$
char *append(char, char *)
The function append takes as function parameters a character to append, and a character pointer (the base address of a character array, a string). append returns a pointer to a string which is composed of the original string with the character supplied as a parameter appended to the end.
Example code:
//John T. Rine //September 23, 2011 #include<stdio.h> #include<stdlib.h> #include "jr.h" int main(int argc, char **argv) { char temp; char *ptrChar; if (argc == 1) printf("Should be at least one command line argument supplied after the file name\n"); else { //only appends to first argument after filename printf("Enter a character to append to the string supplied on the command line\n"); temp = getchar(); printf("%s\n", ptrChar = append(temp, *(argv + 1))); free(ptrChar); } return(0); }
Output:
lab46:~/Library$ ./appendL Joh Enter a character to append to the string supplied on the command line n John lab46:~/Library$
char *prepend(char, char *)
The function prepend takes as function parameters a character to prepend, and a character pointer (the base address of a character array, a string). prepend returns a pointer to a string which is composed of the original string with the character supplied as a parameter prepended to the beginning of the string.
Example code:
//John T. Rine //September 23, 2011 #include<stdio.h> #include<stdlib.h> #include "jr.h" int main(int argc, char **argv) { char temp; char *ptrChar; if (argc == 1) printf("Should be at least one command line argument supplied after the file name\n"); else { printf("Enter a character to prepend to the string supplied on the command line\n"); temp = getchar(); printf("%s\n", ptrChar = prepend(temp, *(argv + 1))); free(ptrChar); } return(0); }
Output:
lab46:~/Library$ ./appendL Joh Enter a character to append to the string supplied on the command line n John lab46:~/Library$ ./prependL ine Enter a character to prepend to the string supplied on the command line R Rine lab46:~/Library$
int occurrences(char *, char)
The function occurrences takes as function parameters a character and a character pointer (the base address of a character array, a string). occurences returns the number of times a particular character occurs in a string up to the maximum size of a integer.
Example code:
//John T. Rine //September 23, 2011 #include<stdio.h> #include "jr.h" int main(int argc, char **argv) { char c; int check; if (argc == 1) printf("Should be at least one command line argument supplied after the file name\n"); else { printf("Enter a character to search the string\n"); c = getchar(); printf("Number of occurrences of %c within the string is %d\n", c, occurrences(*(argv + 1), c)); } return 0; }
Output:
lab46:~/Library$ ./occurrencesL "John Thomas Rine" Enter a character to search the string n Number of occurrences of n within the string is 2 lab46:~/Library$
int find(char *, char)
The function find takes as function parameters a character and a character pointer (the base address of a character array, a string). find returns the first position in the string supplied as a parameter up to the maximum size of an integer.
Example code:
//John T. Rine //September 23, 2011 #include<stdio.h> #include "jr.h" int main(int argc, char **argv) { char c; int check; if (argc == 1) printf("Should be at least one command line argument supplied after the file name\n"); else { printf("Enter a character to search the string\n"); c = getchar(); check = find(*(argv + 1), c); if (check != -1) printf("First position of the character within the string is %d\n", check); else printf("Character not found\n"); } return 0; }
Output:
lab46:~/Library$ ./findL Should be at least one command line argument supplied after the file name lab46:~/Library$ ./findL "Four score and seven years ago" Enter a character to search the string s First position of the character within the string is 5 lab46:~/Library$
char *insert(char, char *, int)
The function insert takes as its function parameters a character and a character pointer (the base address of a character array, a string) and an integer representing the position in the string where the character is to be inserted. insert returns a pointer to a string which is composed of the original string with the character supplied as a parameter inserted at the position parameter.
Example code:
//John T. Rine //September 23, 2011 #include<stdio.h> #include<stdlib.h> #include "jr.h" int main(int argc, char **argv) { char temp = 127, temp2; int number; char *ptrChar; int count; if (argc == 1) printf("Should be at least one command line argument supplied after the file name\n"); else { printf("Enter a character to insert into the string supplied on the command line\n"); temp = getchar(); getchar(); //flush!!! printf("Enter a number representing the position in which to place the character\n"); temp2 = getchar(); getchar(); //flush!!! number = temp2 - 48; printf("%s\n", ptrChar = insert(temp, *(argv + 1), number)); free(ptrChar); } return(0); }
Output:
lab46:~/Library$ ./insertL "John homas" Enter a character to insert into the string supplied on the command line T Enter a number representing the position in which to place the character 5 John Thomas lab46:~/Library$
char *concat(char *, char *)
The function concat takes as its function parameters two character pointers (the base addresses of a character arrays-strings). concat returns a character pointer to a string composed of the second string concatenated to the first, that is it tacks the second string has been tacked onto the end of the first.
Example code:
//John T. Rine //September 23, 2011 #include<stdio.h> #include<stdlib.h> #include "jr.h" int main(int argc, char **argv) { char *ptrString; printf("%s\n", ptrString = concat(*(argv + 1), *(argv + 2))); free(ptrString); return 0; }
Output:
lab46:~/Library$ ./concatL "John Thomas" " Rine" John Thomas Rine lab46:~/Library$
char *toLower(char *)
The function toLower takes as its function parameter a character pointer (the base address of a character array, a string). It returns a character pointer to a string whose letters have been converted to lower case.
Example code:
#include<stdio.h> #include<stdlib.h> #include "jr.h" int main(int argc, char **argv) { char temp; char *ptrChar; if (argc != 2) printf("Should be one command line argument supplied after the file name\n"); else { printf("%s\n", ptrChar = toLower(*(argv + 1))); free(ptrChar); } return 0; }
Output:
lab46:~/Library$ ./toLowerL "JoHn thOmas rInE" john thomas rine lab46:~/Library$
char *toUpper(char *)
The function toUpper takes as its function parameter a character pointer (the base address of a character array, a string). It returns a character pointer to a string whose letters have been converted to upper case.
Example code:
#include<stdio.h> #include<stdlib.h> #include "jr.h" int main(int argc, char **argv) { char temp; char *ptrChar; if (argc != 2) printf("Should be one command line argument supplied after the file name\n"); else { printf("%s\n", ptrChar = toUpper(*(argv + 1))); free(ptrChar); } return 0; }
Output:
lab46:~/Library$ ./toUpperL "jOhN rInE" JOHN RINE lab46:~/Library$
//jr.h //John T. Rine //September 23, 2011 #ifndef _JRLIB_H #define _JRLIB_H int cntChars(char *); char *append(char, char *); char *prepend(char, char *); int occurrences(char *, char); int find(char *, char); char *insert(char, char *, int); char * concat(char *, char *); char *toLower(char *); char *toUpper(char *); #endif
//jr.c //John T. Rine //September 23, 2011 //lab46:~$ gcc -c jr.c -o jr.o //lab46:~$ ar crs libjr.a jr.o //lab46:~$ gcc -I. testStrings.c -o testStrings libjr.a #include<stdlib.h> #include<stdio.h> int cntChars(char *inputString) { // returned value "count" does not include '\0' int count = 0; while(*(inputString + count) != '\0') ++count; return(count); } char *append(char inputChar, char *inputString) { //element 0 1 2 //count = 0 '\0' //count = 1 A '\0' //count = 2 M y '\0' int count, i; char * pChar; count = cntChars(inputString); if (!(pChar = (char *) malloc(sizeof(char) * (count + 2)))) { fprintf(stderr,"Insufficient memory"); exit(1); } else { for(i = 0; i <= (count - 1); i++) *(pChar + i) = *(inputString + i); *(pChar + count) = inputChar; if(inputChar != '\0') *(pChar + count + 1) = '\0'; return(pChar); } } char *prepend(char inputChar, char *inputString) { //element 0 1 2 //count = 0 '\0' //count = 1 A '\0' //count = 2 M y '\0' int count, i; char * pChar; count = cntChars(inputString); if (!(pChar = (char *) malloc(sizeof(char) * (count + 2)))) { fprintf(stderr,"Insufficient memory"); exit(1); } else { *pChar = inputChar; for(i = 1; i <= (count + 1); i++) *(pChar + i) = *(inputString + i - 1); return(pChar); } } int occurrences(char *inputString, char c) { int i = 0; int r = 0; while (*(inputString + i) != '\0') { if(*(inputString + i) == c) r++; i++; } return r; } int find(char *inputString, char c) { int i = 0; int r = -1; while (*(inputString + i) != '\0') { if(*(inputString + i) == c) { r = i; break; } i++; } return r; } char *insert(char inputChar, char *inputString, int position) { //element 0 1 2 //count = 0 '\0' //count = 1 A '\0' //count = 2 M y '\0' int count, i; char * pChar; count = cntChars(inputString); if (!(pChar = (char *) malloc(sizeof(char) * (count + 2)))) { fprintf(stderr,"Insufficient memory"); exit(1); } else { i = 0; while(i <= (count + 1)) { if(i < position) *(pChar + i) = *(inputString + i); else if (i == position) *(pChar + i) = inputChar; else *(pChar + i) = *(inputString + i - 1); i++; } return(pChar); } } char * concat(char *s1, char *s2) { char *r = NULL; int count = 0; int countTotal = 0; int i; int j = 0; count = cntChars(s1); countTotal = count + cntChars(s2); if(!(r = (char *) malloc(sizeof(char) * (countTotal + 1)))) { fprintf(stderr,"Insufficient memory"); exit(1); } else { for(i = 0 ; i < count; i++) *(r + i) = *(s1 + i); while(i < countTotal) { *(r + i) = *(s2 + j); j++; i++; } *(r + countTotal) = '\0'; return r; } } char *toLower(char *inputString) { int count, i; char * pChar; count = cntChars(inputString); if (!(pChar = (char *) malloc(sizeof(char) * (count + 1)))) { fprintf(stderr,"Insufficient memory"); exit(1); } else { for(i = 0; i <= count; i++) { if(*(inputString + i) >= 'A' && *(inputString + i) <= 'Z') *(pChar + i) = *(inputString + i) + 32; else *(pChar + i) = *(inputString + i); } return(pChar); } } char *toUpper(char *inputString) { int count, i; char * pChar; count = cntChars(inputString); if (!(pChar = (char *) malloc(sizeof(char) * (count + 1)))) { fprintf(stderr,"Insufficient memory"); exit(1); } else { for(i = 0; i <= count; i++) { if(*(inputString + i) >= 'a' && *(inputString + i) <= 'z') *(pChar + i) = *(inputString + i) - 32; else *(pChar + i) = *(inputString + i); } return(pChar); } }