User Tools

Site Tools


user:jr018429:portfolio:discrete_structures

DISCRETE STRUCTURES

John T. Rine

09/11/2011

Set Library

John T. Rine
Discrete Structures
Fall 2011

My library is available at:
http://lab46.corning-cc.edu/~jr018429/setLibrary.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:

set.h
set.h is the header file for the set library. It should be included in any .c library files whose function prototypes have been added to it. Also, it must be added to any programs utilizing the library's functions.
Example code:

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

set parseSet(char *)
parseSet.c contains the function definition for the function that parses an input string and creates, if possible, a set from the result of the parsing. parseSet contains the set library function int checkSetNotation(char *inputString)which checks that the input string is in proper set notation, if it isn't, an error is returned.
Example code:

parseSet.c
//parseSet.c
//John T. Rine
//October 15, 2011
 
#include "set.h"
#include<stdlib.h>
 
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;
}

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:

cntChars.c
//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);
}

int checkSetNotation(char *)
checkSetNotation.c checks that the input string is in proper set notation, if it isn't, an error is returned.

checkSetNotation.c
//checkSetNotation.c
//John T. Rine
//October 15, 2011
 
#include"set.h"
#include<stdio.h>
 
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;
}

The following setTest execution illustrates the capabilities of the checkSetNotation function.
This function checks for the following conditions:

  • beginning brace
  • ending brace
  • beginning brace and adjacent comma (error)
  • adjacent comma and ending brace (error)
  • adjacent commas (error)

The function therefore will ignore spaces. It should treat them as though they are part of an element of a set.

c:\MinGW\jrprogs\set\setProject>setTest {A,B,C,D,E}
{A,B,C,D,E}

c:\MinGW\jrprogs\set\setProject>setTest A,B,C,D,E}
First character in a set representation must be a '{'

c:\MinGW\jrprogs\set\setProject>setTest {,B,C,D,E}
First two characters in a set representation can't be "{,"

c:\MinGW\jrprogs\set\setProject>setTest {A,,C,D,E}
Can't have two adjacent commas in a set representation

c:\MinGW\jrprogs\set\setProject>setTest {A,B,,D,E}
Can't have two adjacent commas in a set representation

c:\MinGW\jrprogs\set\setProject>setTest {A,B,C,,E}
Can't have two adjacent commas in a set representation

c:\MinGW\jrprogs\set\setProject>setTest {A,B,C,D,}
Last two characters in a set representation can't be ",}"

c:\MinGW\jrprogs\set\setProject>setTest {A,B,C,D,E
Last character in a set representation must be a '}'

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:

occurrences.c
//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;
}

void displaySet(set)
displaySet.c takes the set set datatype which consists of a structure wrapping a pointer to a two dimensional array and an integer to store the number of elements in the set, and formats and prints it in proper set notation.
Example code:

displaySet.c
//displaySet.c
//John T. Rine
//October 15, 2011
 
#include"set.h"
#include<stdio.h>
 
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");
}

setTest.c
setTest.c is the test file that utilizes the set library functions.
Example code:

setTest.c
//setTest.c
//John T. Rine
//October 16, 2011
#include<stdio.h>
#include<stdlib.h>
#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;
 
} 

makeSetTest.bat
The Windows batch file makeSetTest.bat automates, manufacture of all of the .o files, the creation of the static library file, compiling, linking, and execution of setTest, the test file.
Example code:

makeSetTest.bat
REM makeSetTest.bat
REM John T. Rine
REM October 16, 2011
del *.exe
del *.o
del *.a
gcc -c displaySet.c -o displaySet.o
gcc -c cntChars.c -o cntChars.o
gcc -c checkSetNotation.c -o checkSetNotation.o
gcc -c occurrences.c -o occurrences.o
gcc -c parseSet.c -o parseSet.o
 
ar src libset.a *.o
gcc -I. setTest.c -o setTest libset.a
SET /P M=What program to run?
setTest %M%
 
pause

Execution of makeSetTest.bat:

C:\MinGW\jrprogs\set\setProject>del *.exe

C:\MinGW\jrprogs\set\setProject>del *.o

C:\MinGW\jrprogs\set\setProject>del *.a

C:\MinGW\jrprogs\set\setProject>gcc -c displaySet.c -o displaySet.o

C:\MinGW\jrprogs\set\setProject>gcc -c cntChars.c -o cntChars.o

C:\MinGW\jrprogs\set\setProject>gcc -c checkSetNotation.c -o checkSetNotation.o


C:\MinGW\jrprogs\set\setProject>gcc -c occurrences.c -o occurrences.o

C:\MinGW\jrprogs\set\setProject>gcc -c parseSet.c -o parseSet.o

C:\MinGW\jrprogs\set\setProject>ar src libset.a *.o

C:\MinGW\jrprogs\set\setProject>gcc -I. setTest.c -o setTest libset.a

C:\MinGW\jrprogs\set\setProject>SET /P M=What program to run?
What program to run?{orange,apple,pineapple,banana,papaya,pear}

C:\MinGW\jrprogs\set\setProject>setTest {orange,apple,pineapple,banana,papaya,pear}
{orange,apple,pineapple,banana,papaya,pear}

C:\MinGW\jrprogs\set\setProject>pause
Press any key to continue . . .

makeSetTest.sh
The bash shell script file makeSetTest.sh automates the manufacture of all of the .o files, the creation of the static library file, compiling, linking and execution of setTest, the test file.
Example code:

makeSetTest.sh
#!/bin/bash
gcc -c checkSetNotation.c -o checkSetNotation.o
gcc -c cntChars.c -o cntChars.o
gcc -c displaySet.c -o displaySet.o
gcc -c occurrences.c -o occurrences.o
gcc -c parseSet.c -o parseSet.o
 
ar scr libset.a *.o
gcc -I. setTest.c -o setTest libset.a
read input
run="./setTest "
concat="$run$input"
$concat

Execution of makeSetTest.sh:

lab46:~/src/discrete$ ./makeSetTest.sh
{apple,orange}
{apple,orange}

String Handling Library

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$

Code

cntChars.c

cntChars.c
//John T. Rine
//September 10, 2011 (Revised)
 
//This function is the basis for a set of string handling routines
//$ ./cntChars Hello Jenny Sue!
//There are 5 characters in string 1
//There are 5 characters in string 2
//There are 4 characters in string 3
//$ ./cntChars "John Rine" "Likes to" Program!
//There are 9 characters in string 1
//There are 8 characters in string 2
//There are 8 characters in string 3
 
#include<stdio.h>
 
int cntChars(char *);
 
int main(int argc, char **argv)
{
	int i;
 
	if (argc > 1) for(i = 1; i <= (argc - 1); i++) printf("There are %d characters in string %d\n", cntChars(*(argv + i)), i);
	else printf("No command line arguments were supplied after the file name\n");
 
	return(0);
}
 
int cntChars(char *inputString)
{
	// returned value "count" does not include
	// '\0'
	int count = 0;
	while(*(inputString + count) != '\0') ++count;
	return(count);
}

append.c

append.c
//John T. Rine
//September 10, 2011
 
#include<stdio.h>
#include<stdlib.h>
 
int cntChars(char *);
char *append(char, char *);
 
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);
}
 
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);
	}
}

prepend.c

prepend.c
//John T. Rine
//September 10, 2011
 
#include<stdio.h>
#include<stdlib.h>
 
int cntChars(char *);
char *prepend(char, char *);
 
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 cntChars(char *inputString)
{
	// returned value "count" does not include '\0'
	int count = 0;
	while(*(inputString + count) != '\0') ++count;
	return(count);
}
 
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);
	}
}

insert.c

insert.c
//John T. Rine
//September 10, 2011
 
#include<stdio.h>
#include<stdlib.h>
 
int cntChars(char *);
char *insert(char, char *, int);
 
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 append to 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);
}
 
int cntChars(char *inputString)
{
	// returned value "count" does not include '\0'
	int count = 0;
	while(*(inputString + count) != '\0') ++count;
	return(count);
}
 
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);
	}
}

concat.c

concat.c
#include<stdio.h>
 
int cntChars(char *);
char * concat(char *, char *);
 
int main(int argc, char **argv)
{
	char *ptrString;
	printf("%s\n", ptrString = concat(*(argv + 1), *(argv + 2)));
	free(ptrString);
	return 0;
}
 
int cntChars(char *inputString)
{
	// returned value "count" does not include '\0'
	int count = 0;
	while(*(inputString + count) != '\0') ++count;
	return(count);
}
 
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;
	}
}

find.c

find.c
#include<stdio.h>
 
int find(char *, char);
 
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;
 
}
 
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;
}

occurrences.c

occurrences.c
#include<stdio.h>
 
int occurrences(char *, char);
 
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 occurrences(char *inputString, char c)
{
	int i = 0;
	int r = 0;
	while (*(inputString + i) != '\0')
	{
		if(*(inputString + i) == c) r++;
		i++;
	}
	return r;
}

toUpper.c

toUpper.c
#include<stdio.h>
#include<stdlib.h>
 
int cntChars(char *);
char *toUpper(char *);
 
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;
}
 
int cntChars(char *inputString)
{
	// returned value "count" does not include
	// '\0'
	int count = 0;
	while(*(inputString + count) != '\0') ++count;
	return(count);
}
 
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);
	}
}

toLower.c

toLower.c
#include<stdio.h>
#include<stdlib.h>
 
int cntChars(char *);
char *toLower(char *);
 
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;
}
 
int cntChars(char *inputString)
{
	// returned value "count" does not include
	// '\0'
	int count = 0;
	while(*(inputString + count) != '\0') ++count;
	return(count);
}
 
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);
	}
}
user/jr018429/portfolio/discrete_structures.txt · Last modified: 2011/10/24 20:33 by jr018429