📄 tstring.c
字号:
//---------------------------------------------------------
/*
File name: Tstring.c
Comments: This file contains the implementation of the
text_string type.
*/
//---------------------------------------------------------
// Include files
#include <stdio.h>
#include "Tstring.h"
// End include files
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringInitString
Parameters
Input: theString - Contains the text_string to be
initialized.
Output: theString - Contains the initialized
text_string.
Return Values: This function does not return a value.
Comments: Before a text_string variable is used, it
must be initialized by this function. Your
program must supply a text_string variable
in the paramter list. This function will
set all of the character locations in the
parameter to the null character. Therefore,
theString is both an input and output
parameter.
*/
void TextStringInitString(text_string theString)
{
int i;
for (i=0;i<TEXT_STRING_MAX_LENGTH;i++)
{
theString[i] = '\0';
}
}
// End TextStringInitString
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringSetString
Parameters
Input: charArray - Contains an array of characters
which will be copied into theString.
Output: theString - Specifies a text_string into
which the characters from charArray
will be copied.
Return Values: Upon success, this function returns the
value TSE_NO_ERROR. If charArray is empty,
it returns TSE_EMPTY_SOURCE_CHAR_ARRAY. If
there are too many characters in charArray
to copy into theString, this function
returns TSE_SOURCE_CHAR_ARRAY_TOO_LONG.
Comments: This function copies characters from the
input parameter charArray to the output
parameter theString.
*/
text_string_error TextStringSetString(
text_string theString,
char charArray[])
{
int i;
text_string_error errorStatus = TSE_NO_ERROR;
// Copy the characters in the array to the text_string.
// Insert a for loop here.
// If the source array was empty...
if (i==0)
{
// Set a warning.
errorStatus = TSE_EMPTY_SOURCE_CHAR_ARRAY;
}
// Else if the source array was too long...
else if (i>=TEXT_STRING_MAX_LENGTH)
{
// Set an error.
errorStatus = TSE_SOURCE_CHAR_ARRAY_TOO_LONG;
}
// Else the source array was copied ok...
else
{
// Append a null character on the end.
theString[i]='\0';
}
return (errorStatus);
}
// End TextStringSetString
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringSetCharacter
Parameters
Input: theCharacter - Contains a character which
will be copied into theString.
characterIndex - Specifies the index number
of the character position in theString
where theCharacter will be copied into.
Index numbers begin with 0.
Output: theString - Specifies a text_string into
which the character will be copied.
Return Values: Upon success, this function returns the
value TSE_NO_ERROR. If index number is out
of range, it returns TSE_INDEX_OUT_OF_RANGE.
If index number is after then end of the
string, this function returns
TSE_INDEX_AFTER_END_OF_STRING.
Comments: This function copies a character into the
output parameter theString. It is possible
for theString to contain less than the
maximum number of characters. If it does,
and the index number is within range but
after the end of the string, this function
returns an error.
*/
text_string_error TextStringSetCharacter(
text_string theString,
char theCharacter,
int characterIndex)
{
text_string_error errorStatus = TSE_NO_ERROR;
int length;
// If the index is out of range...
if ((characterIndex < 0) ||
(characterIndex>=TEXT_STRING_MAX_LENGTH-1))
{
// Set an error.
errorStatus = TSE_INDEX_OUT_OF_RANGE;
}
else
{
// Find the current length of the text_string.
for (length = 0;
(length<TEXT_STRING_MAX_LENGTH-1) &&
(theString[length]!='\0');
length++)
{
/* The body is empty. The counting is done in
the control portion of the for loop. */
}
/* If the character index is after the end
of the string... */
if (characterIndex>=length)
{
// Set an error.
errorStatus = TSE_INDEX_AFTER_END_OF_STRING;
}
else
{
// Set the character.
theString[characterIndex]=theCharacter;
}
}
return (errorStatus);
}
// End TextStringSetCharacter
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringGetCharacter
Parameters
Input:
Output:
Return Values:
Comments:
*/
int TextStringGetCharacter(text_string theString,
int characterIndex)
{
int length;
int theCharacter = -1;
// If the index is in range...
{
// Find the current length of the text_string.
/* If the index is not beyond the end of the
string... */
// Get the character.
}
return (theCharacter);
}
// TextStringGetCharacter
//---------------------------------------------------------
text_string_error TextStringAppendCharacter(
text_string theString,
char theCharacter)
{
text_string_error errorStatus = TSE_NO_ERROR;
int length;
// Find the position of the null character.
// If the string is full...
{
// Set an error
}
else
{
// Append the character.
// Put the null character on the end.
}
return (errorStatus);
}
int TextStringPrintString(text_string theString)
{
return (printf("%s",theString));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -