C++: Fulfilling the requirements: //stringPkg.h//A library of ASCIIZ string func
ID: 3797171 • Letter: C
Question
C++:
Fulfilling the requirements:
//stringPkg.h//A library of ASCIIZ string functions.//All functions in this library manipulate character arrays//whose values are sequences of characters (string-values)//utilizing the ASCIIZ format.//----------------//Return length of a string, length > =0.//Length is a count of the characters in the string-value, //not including the null at the end.//An empty string (" ") has length theta. int stringLength (const char s[]);//---------------//Copy string src to string dest void string Copy (char dest [], const int destBuffsize, const char src []);//-----------//Concatenate string t to end of string dest void string Concatenate (char dest [], const int destBuffsize, const char t[]);//---------------//Retrieve character from string s[position] Position must be between 0 and (stringLength-1).//Return 0 if position is out of range char stringGetchar (const char s[], const int position);//---------------//Find ch in string s starting a the "startpos" array location.//startpos must be non-negative and less than the string length.//Return the 'found' position: 0 - (stringLength-1)//Return -1 if ch not found in s. int stringFindchar (const char s[], const char ch, const int startpos = 0);//---------------//Set resultString [] to a string value that is a copy of//a specified substring of original string s.//If specified start position is not located within the string s, //then set resultString to the empty string (" ").//If specified len the length of s//then set resultstring to the longest possible substring. Void stringSubstring(char resultString[], //new string buffer const int resultBuffSize, //result array biffer size const int s[], //starting position of substring within s const int len = -1);//length of substring within s//lenExplanation / Answer
The header file containing all the function definitions is:
Code:
#ifndef stringPkg_H_INCLUDED
#define stringPkg_H_INCLUDED
int stringLength(const char s[])
{
int i=0;
int length=0;
for(i=0; s[i]!=''; i++) // At the end of string, we will have NULL character which is same as ''
{
length++;
}
// length is initialized to 0. So, if empty string is there, the for loop condition will be false so it won't go inside loop.
// So, 0 will be returned by default.
return length;
}
void stringCopy(char dest[], const int destBuffSize, const char src[])
{
int srclen = stringLength(src);
if(destBuffSize < srclen)
{
cout << "String will not be able to be copied completely, it will be copied till its capacity";
}
//starting index for a string is 0. So, from 0 to destBuffSize, we need to copy the string.
//Moreover, if the size is more than srclen then we need to stop when the src[i] becomes null i.e. the complete string is copied.
for(int i=0; i<destBuffSize && src[i]!=''; i++)
{
dest[i] = src[i];
}
}
void stringConcatenate(char dest[], const int destBuffSize, const char t[])
{
int destlen = stringLength(dest);
int tlen = stringLength(t);
if(destlen + tlen > destBuffSize)
{
cout << "Concating the string, but it will not be concated completely as the total size is less than destination capacity."
}
int j=0; //j is the index for string t[] to traverse through its characters and append them to dest
// we have destlen that contains number of characters of dest[]. So, to concat t[], we need to start from the last.
//for dest[], the index is 0 to destlen-1. So, copying should be started from destlen index itself.
//Moreover, we need to concat until there is a character present in t[]. It is possible that destBuffSize is more than total required.
//So we need to concat till destBuffSize as well as there's a character present in t[].
for(int i=destlen; i<destBuffSize && t[j]!=''; i++)
{
dest[i] = t[j++];
}
}
char stringGetchar(const char s[], const int position)
{
int slen = stringLength(s);
// we need to check if position is not negative and it is less than slen because starting index remains 0 in every string.
if(!(position >=0 && position < slen))
{
cout << "Invalid position entered.";
return 0;
}
/*if the position is valid, we need to return the character at that position. Like, 4th position then we need to return
s[3] as s[0] contains 1st character, ... and so on. So, it becomes position-1 index need to be returned.
*/
return s[position-1];
}
int stringFindchar(const char s[], const char ch, const int startpos = 0)
{
//Here, startpos is given 0 as default argument. So, you can call this with or without providing startpos from main().
// this will return first occurence of character if present in the string.
int slen = stringLength(s);
// checking whether startpos is negative or it is more than or equal to the string length, then returning -1.
if(!(startpos >=0 && startpos < slen))
{
cout << "Invalid startpos entered.";
}
else
{
for(int i=startpos; i<slen; i++)
{
if(s[i] == ch)
{
return i;
}
}
}
// Here control comes that means character is not found. Invalid startpos also means the character is not found.
return -1;
}
void stringSubstring(char resultString[], const int resultBuffSize, const char s[], const int start, const int len = -1)
{
// here, if len = -1 then we need to copy till the end of s[] in resultString[].
int slen = stringLength(s);
// if start is not found in s i.e. if start is greater than the number of characters in s[] then "" is stored as resultString.
//also here it is good to ensure that start is valid, i.e. it is non-negative number.
if(start <0 || start >= slen)
{
resultString = "";
return; // this means return to the calling function, this does not return any value.
}
if(len < 0)
{
cout << "As len < 0, returning longest possible substring.";
int j=start-1;
/* from start-1 i.e. if start = 5, start from s[4], copying till slen i.e. there is a character available in s[].
So, all the comparison is done on the value of j. i.e. j=start-1 to j=slen-1, we need to find the substring.
*/
for(int i=0; j< slen; i++)
{
resultString[i] = s[j++];
}
}
else if(start + len > resultBuffSize)
{
cout << "Returning longest possible substring as the length is less than the destination buffer size.";
// As here the capacity is less than total asked for start+len, we need to return till resultBuffSize
int j=start-1;
for(int i=0; i< resultBuffSize; i++)
{
resultString[i] = s[j++];
}
}
//normal condition so return as per desired. The code is similar to above without printing anything and returning till length-1
else
{
int j=start-1;
for(int i=0; i < len; i++)
{
resultString[i] = s[j++];
}
}
}
int stringCompare(const char s[], const char t[])
{
// comparison is done till both the string has a character.
for(int i=0; s[i]!='' && t[i]!=''; i++)
{
// compares ASCII values of both s[i] and t[i]
// if both characters are equal, we need to check this further till we found the similarilities. At the end only we can
// conclude if both the strings are same or not. So using continue to continue the loop.
if(s[i]==t[i])
{
continue;
}
else
{
// if s[i] > t[i] it will return positive value otherwise it will return negative value.
// And the comparison get ended here.
return s[i] - t[i];
}
}
// if the control has reached till here, it means that all the charcters are same in both the strings. So return 0.
return 0;
}
#endif
Note: I have entered comments at all the places in the code itself so that the understanding becomes easier. If there is any query, do comment. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.