// Author: Nicholas Dornelles Franceschini
// Filename:
// Date: September, 2014
// Compiler: Quincy 2005 ver. 1.3
// OS: Windows 7 SP1
// Assigned: September 9th
// Due: September 19th
// Description: This program will mimic the cstring header
// file, giving access to functions that will
// count the characters in a string, compare
// strings, copy one string onto another and
// concatenate two strings.
///////////////////////////////////////////////////////////
// System header files
#include <iostream>
// C-String Function Prototypes
int ndfStrLen(const char * const);
void ndfStrCpy(char * const, const char * const);
void ndfStrCat(char [], const char []);
int ndfStrCmp(const char [], const char []);
void ndfOutLen(const char * const, const char * const);
// control function
int main()
{
char array1[100] = "lol";
char array2[100] = "lol";
std::cout << "Please enter two strings. Hit enter after each one." << std::endl;
std::cin.getline(array1, 99, '\n');
std::cin.getline(array2, 99, '\n');
std::cout << std::endl;
ndfOutLen(array1, array2);
std::cout << std::endl;
ndfStrCmp(array1, array2);
std::cout << std::endl;
ndfStrCpy(array2, array1);
std::cout << array1 << std::endl;
std::cout << array2 << std::endl;
std::cout << std::endl;
ndfOutLen(array1, array2);
std::cout << std::endl;
ndfStrCat(array2, array1);
std::cout << "String \"" << array2 << "\" is " << ndfStrLen(array2) << " characters long." << std::endl;
ndfStrCmp(array1, array2);
std::cout << std::endl;
return 0;
}
///////////////////////////////////////////////////////////
// Purpose: This function counts the number of characters
// in a string.
// Input: One char array containing a string.
// Output: An integer of value equal to the number of
// characters, minus the null, in the provided
// char array.
int ndfStrLen(const char * const chArray1)
{
int counter = 0;
char content = '\0';
bool isNull = false;
// This loops through the char array until it finds a
// null char. If it does not, it adds one to counter,
// if it does, isNull becomes TRUE, closing the loop
// and returning counter, which will contain a number
// equivalent to the number of chars excluding the \0
while(isNull == false)
{
content = *(chArray1 + counter);
if(content == '\0')
{
isNull = true;
}
counter++;
}
// returns the total number of chars in the array minus
// the null char.
return counter - 1;
}
///////////////////////////////////////////////////////////
// Purpose: This function copies the second char array
// into the first, overwriting what it originally
// had in it and assuming it is big enough.
// Input: Two char arrays.
// Output: None, this function will edit the array.
void ndfStrCpy(char * const chArray1, const char * const chArray2)
{
int counter = 0;
char content = '\0';
bool isNull = false;
// This loops through the second char array, copying
// the char into the first array, until it finds a
// null char. If it does not; it adds one to counter,
// if it does; isNull becomes TRUE, closing the loop.
while(isNull == false)
{
content = *(chArray2 + counter);
*(chArray1 + counter) = content;
if(content == '\0')
{
isNull = true;
}
counter++;
}
return;
}
///////////////////////////////////////////////////////////
// Purpose: This funtion adds the second char array to the
// first one and assumes that there is enough
// space for it.
// Input: Two char arrays.
// Output: None, this function will edit the array.
void ndfStrCat(char chArray1[], const char chArray2[])
{
int counter = 0;
char content = '\0';
bool isNull = false;
int length = 0;
// This figures out the size of the first array
length = ndfStrLen(chArray1);
// This loops through both arrays, assigning chars from
// second array to the end of the first array.
while(isNull == false)
{
content = chArray2[counter];
chArray1[length + counter] = content;
if(content == '\0')
{
isNull = true;
}
counter++;
}
return;
}
///////////////////////////////////////////////////////////
// Purpose: This function will compare 2 char arrays.
// Input: Two char arrays.
// Output: An integer; positive if array 1 is bigger, 0 if
// they are equal or negative is array 2 is bigger
int ndfStrCmp(const char chArray1[], const char chArray2[])
{
int counter = 0;
char contentArr1 = '\0';
char contentArr2 = '\0';
bool isDone = false;
int result = 0;
while(isDone == false)
{
contentArr1 = chArray1[counter];
contentArr2 = chArray2[counter];
if(contentArr1 == '\0' || contentArr2 == '\0')
{
isDone = true;
}
else
{
result = contentArr1 - contentArr2;
if(result > 0)
{
isDone = true;
std::cout << chArray1 << " is greater than " << chArray2 << "." << std::endl;
}
else if(result < 0)
{
isDone = true;
std::cout << chArray1 << " is less than " << chArray2 << "." << std::endl;
}
}
if(result == 0)
{
std::cout << "The strings are equal." << std::endl;
}
counter++;
}
return result;
}
///////////////////////////////////////////////////////////
// Purpose: This function prints both arrays and their
// lengths.
// Input: Two char arrays.
// Output: Array and size to screen, nothing internally.
void ndfOutLen(const char * const chArray1, const char * const chArray2)
{
std::cout << "String \"" << chArray1 << "\" is " << ndfStrLen(chArray1) << " characters long." << std::endl;
std::cout << "String \"" << chArray2 << "\" is " << ndfStrLen(chArray2) << " characters long." << std::endl;
return;
}