STRING AND CHARACTER MANIPULATION LIBRARY
John T. Rine
11/01/2011
======Objectives======
My character and string handling library is available at:\\
http://lab46.corning-cc.edu/~jr018429/Library.zip\\
\\
The objective of this project is to create a character and string handling library with which to write programs that process and manipulate character and strings. This project is a requirement of the Discrete Structures course and is a prerequisit for the set library project.
======Prerequisites======
In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
* understand and use the C/C++ programming language
* understand and use functions
* understand and use pointers in general
* understand and use malloc() to dynamically allocate memory
* understand and use free() to deallocate memory
* know how to create and use a static library
======Background======
Come on! everyone knows thtat ther is no "background" in one of Joe Oppenheim'd courses....
However, once we were assigned the set library project (see the set library project) the character and string library made more sense.
The purpose of creating this library was so that they could be used in the set library. Actually, I think Joe intended these functions to act as part of the set library; I believe he origninally intended too use characters as elements of sets, I believe he changed his mind and decided strings would be used as set elements and the string and character library could then be used in implementation of a set library which uses strings as elements.\\
======Scope======
A defined by the course instructor, Joe Oppenheim, the character and string library is to include routines which:
* Returns the count for the number of occurences of a given character in a string
* Appends a character to the end of a string
* Prepends a character to the beginning of a string
* Inserts a character at a given position in a string
* Returns a string which is the concatenation of one string to the end of another
\\
I added a routines to convert strings to lower and to convert to upper case.\\
======Attributes======
The course requirement for Data Structures projects are listed at:\\
http://lab46.corning-cc.edu/haas/fall2011/data/projects
This project has the following project attributes described on that page:
^Attribute^Qty Needed^Description^
|Pointers|8|Indirection/dereferencing are demonstarted|
|malloc/new|4|Utilization of dynamic memory allocation|
|Libraries|4|Implementation/utilization of non-core libc library|
\\
This project, therefore, is eligible for 16 attributes total, however, only 6 of these are achieveable per project, so project far exceeds the minimum attribut requirements.\\
======Code======
The actual library files are jr.h and jr.c:\\
//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
#include
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);
}
}
\\
The library was developed by first writing, compiling, linking, testing, and debugging the following individual programs for each library function.\\
\\
**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
#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;
}
\\
**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
#include
#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);
}
\\
**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
#include
#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);
}
\\
**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
#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;
}
\\
**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
#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;
}
\\
**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
#include
#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);
}
\\
**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
#include
#include "jr.h"
int main(int argc, char **argv)
{
char *ptrString;
printf("%s\n", ptrString = concat(*(argv + 1), *(argv + 2)));
free(ptrString);
return 0;
}
\\
**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
#include
#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;
}
\\
**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
#include
#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;
}
\\
======Execution======
Execution of cntChars:\\
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$
\\
Execution of appendL:\\
lab46:~/Library$ ./appendL Joh
Enter a character to append to the string supplied on the command line
n
John
lab46:~/Library$
\\
Execution of prependL:\\
lab46:~/Library$ ./prependL ine
Enter a character to prepend to the string supplied on the command line
R
Rine
lab46:~/Library$
\\
Execution of occurences:\\
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$
\\
Execution of find:\\
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$
\\
Execution of insertL:\\
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$
\\
Execution of concatL:\\
lab46:~/Library$ ./concatL "John Thomas" " Rine"
John Thomas Rine
lab46:~/Library$
\\
Execution of toLowerL:\\
lab46:~/Library$ ./toLowerL "JoHn thOmas rInE"
john thomas rine
lab46:~/Library$
\\
Execution of toUpperL:\\
lab46:~/Library$ ./toUpperL "jOhN rInE"
JOHN RINE
lab46:~/Library$
======Reflection======
It wasn't clear, when the the class was assigned the task of creating a character and string handling library, what the purpose of such an exercise was. However, once the set library assignment was given, it became clear that this library could be used to create the set library. So the purpose of this exercise was to prepare for the set library exercise.\\
======References======
* http://en.wikipedia.org/wiki/String_(computer_science)
\\