Fixed most of the problems with the StringUtils

+- Phase6_Prep.c
+- StringUtils.c

Note: Lev did things wrong using malloc in the videos. Corrections have been made.

Wrong: malloc( sizeof( sourceStrLen + 1 ) )
Correct: malloc( sizeof(char) * (sourceStrLen + 1) )

This will correctly fix most of the dynamic memory allocation problems that will arise from programs described in the video.
This commit is contained in:
Charles Descamps 2024-01-07 22:04:12 -07:00
parent cb03243373
commit 08677204ca
2 changed files with 7 additions and 5 deletions

View file

@ -66,10 +66,12 @@ int main()
printf("\nLowercase String: '%s'\n", testStringFour);
// string get to delimiter
// stringDelimiter
printf("\nAbout to get string to delimiter -> '.' from standard in:\n");
getStringToDelimiter(stdin, '.', testStringFive);
printf("\nCaptured to delimiter: '%s'\n", testStringFive);
// string get to end of line
// stringEOL
printf("\nAbout to get string to line end from standard in:\n");
getStringToLineEnd(stdin, testStringFive);
printf("\nCaptured to end of line: '%s'\n", testStringFive);

View file

@ -220,14 +220,14 @@ bool getStringConstrained(FILE *inStream, bool clearLeadingNonPrintable,
char delimiter, char *capString)
{
// initialize variables
int intChar = -1, index = 0;
int intChar = EOF, index = 0;
// initialize output string
capString[index] = NULL_CHAR;
// capture first value in stream using fgetc
intChar = fgetc(inStream);
// loop to clear non-printable or space if indicated
while((((intChar != -1) &&
while((((intChar != EOF) &&
(clearLeadingNonPrintable && (intChar < (int)SPACE)))
|| (clearLeadingSpace && (intChar == (int)SPACE))))
{
@ -236,13 +236,13 @@ bool getStringConstrained(FILE *inStream, bool clearLeadingNonPrintable,
}
// end loop to clear non-printable or space if indicated
// check for end of file found
if(intChar == -1)
if(intChar == EOF)
{
// return false (failed)
return false;
}
// loop to get input from file stream
while(((intChar != -1 && index < MAX_STR_LEN - 1) &&
while(((intChar != EOF && index < MAX_STR_LEN - 1) &&
(stopAtNonPrintable && intChar >= (int)SPACE))
|| ((!stopAtNonPrintable) && (intChar != (char)delimiter)))
{
@ -400,7 +400,7 @@ void strToLowerCase(char *outString, const char *inString)
int index = 0;
// copy source string in case of aliasing using malloc and copyString
tempStr = (char *)malloc((sizeof(sourceStrLen + 1)));
tempStr = (char *)malloc((sizeof(char) * (sourceStrLen + 1)));
copyString(tempStr, inString);
// loop across source string
while(tempStr[index] != NULL_CHAR && index < MAX_STR_LEN)