// [Description] #include <iostream> // Function Prototypes=====================
ID: 665000 • Letter: #
Question
// [Description]
#include <iostream>
// Function Prototypes=========================================================
// Do NOT change the signature of these function prototypes.
// If you do, your code will not compile with my altered main() function
// I suggest that you copy the prototypes below and then fill them in.
// ----------------------------------------------------------------------------
// Read in a line of text INCLUDING SPACES into a string.
// You may assume that the input will NOT exceed the maxLength available.
// Keep in mind that cin stops reading at a whitespace. See page 318.
void ReadString(char * c, int maxLength);
// Get the length of the string and store it in length
// Hint: How does C++ terminate a string? Look for that character!
void GetStringLength(char * c, int * length);
// PrintString - Just print the string in forward order using cout
void PrintString(char * const c);
// PrintStringBackwards - Print the string in reverse order using cout
void PrintStringBackwards(char * const c);
// Return a pointer to the character at the index given
char * GetValueAtIndex(char * const c, int index);
// Return true/false if the string contains the testVal
bool SearchString(char * c, char testVal);
// ============================================================================
//[BEGIN MAIN] -- Don't delete this line
int main()
{
// Use these variables to test.
// SIZE could change so make sure your code works with different sizes.
const int SIZE = 100;
char ca[SIZE];
char * pc = ca;
// Your code below
// =========================
// =========================
// Your code above
std::cout << "Press ENTER";
std::cin.get();
return 0;
}
//[END MAIN] -- Don't delete this line
void ReadString(char * c, int maxLength)
{
std::cout << "Enter a string less than " << maxLength << " characters." << std::endl;
// Your code here
// ==============================
// ==============================
}
// You need to provide the rest of the functions
// See Function Prototypes at the top.
Explanation / Answer
#include <iostream>
void ReadString(char * c, int maxLength);
// Get the length of the string and store it in length
// Hint: How does C++ terminate a string? Look for that character!
void GetStringLength(char * c, int * length);
// PrintString - Just print the string in forward order using cout
void PrintString(char * const c);
// PrintStringBackwards - Print the string in reverse order using cout
void PrintStringBackwards(char * const c);
// Return a pointer to the character at the index given
char * GetValueAtIndex(char * const c, int index);
// Return true/false if the string contains the testVal
bool SearchString(char * c, char testVal);
int main()
{
// Use these variables to test.
// SIZE could change so make sure your code works with different sizes.
const int SIZE = 80;
char ca[SIZE];
char * pc = ca;
int fPrints = 0;
int bPrints = 0;
int lengthChecks = 0;
// Your code below
// =========================
char selection = 'z';
while(selection != 'Q')
{
std::cout << "[1] Read in a string" << std::endl;
std::cout << "[2] Print string" << std::endl;
std::cout << "[3] Print string in reverse" << std::endl;
std::cout << "[4] Search string" << std::endl;
std::cout << "[5] Get string length" << std::endl;
std::cout << "[6] Replace value at index" << std::endl;
std::cout << "[Q] Quit" << std::endl;
std::cout << "Selection: ";
std::cin >> selection;
std::cin.ignore();
std::cout << std::endl;
switch(selection)
{
case '1':
ReadString(pc, SIZE);
break;
case '2':
fPrints += 1;
std::cout << "Forward[" << fPrints << "]=";
PrintString(pc);
std::cout << std::endl;
break;
case '3':
bPrints += 1;
std::cout << "Backwards[" << bPrints << "]=";
PrintStringBackwards(pc);
std::cout << std::endl;
break;
case '4':
{
char searchChar = ' ';
std::cout << "Search for what?: ";
std::cin >> searchChar;
std::cout << std::endl;
std::cout << "Contains(" << searchChar << ")=" << SearchString(pc, searchChar) << std::endl;
break;
}
case '5':
{
lengthChecks += 1;
int length = 0;
GetStringLength(pc, &length);
std::cout << "Length[" << lengthChecks << "]=" << length << std::endl;
break;
}
case '6':
{
int index = -1;
char newChar = ' ';
char * cPtr;
std::cout << "What index?: ";
std::cin >> index;
std::cout << "New char?: ";
std::cin >> newChar;
cPtr = GetValueAtIndex(pc, index);
*cPtr = newChar;
break;
}
case 'Q':
break;
default:
break;
}
}
// =========================
// Your code above
std::cout << "Press ENTER";
std::cin.get();
return 0;
}
void ReadString(char * c, int maxLength)
{
std::cout << "Enter a string less than " << maxLength << " characters." << std::endl;
// Your code here
// ==============================
std::cin>>c;//take in input (including whitespace), up to maxLength, terminate at newline and assign to pointer c
// ==============================
}
// You need to provide the rest of the functions
// See Function Prototypes at the top.
void GetStringLength(char * c, int * length)
{
for(int i = 0; i < *length; i++)//Linear Search for Null Character (Last char in array)
{
if(c[i] == '')//Search each element for null character
*length = i - 1; //When the null character is found, the address length points to is assigned i
}
}
void PrintString(char * const c)
{
int counter = 0;
for(int i = 0; i < 100; i++)//This snippet is copy and pasted from GetStringLength to get the length of the string
{
if(c[i] == '')
{
counter = i;//dont want to include the null terminator
break;
}
}
for(int j = 0; j < counter; j++)//For the length of the string
{
std::cout << c[j]; //Print each element
if(j < counter)
std::cout << '';
}
std::cout << std::endl;
}
void PrintStringBackwards(char * const c)
{
int counter = 0;
for(int i = 0; i < 100; i++)//This snippet is copy and pasted from GetStringLength to get the length of the string
{
if(c[i] == '')
{
counter = i;//dont want to include the null terminator
break;
}
}
int s, i , j;
for(int i = 0, j = counter - 1; i < j; i++, j--)//i starts at the beginning of the array (0) and j starts at the end (minus the null character)
{
s = c[i];//temporary hold the value
c[i] = c[j]; //swap
c[j] = s; //swap
}
}
char * GetValueAtIndex(char * const c, int index)
{
return &c[index]; //return the address of array c at index
}
bool SearchString(char * c, char testVal)
{
for(int i = 0; i < 100; i++)//Linear Search, start at element 0 and go to the end
{
if(c[i] == testVal) //if the element currently being looked at is equivalent to testVal
return true;
}
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.