Concept explainers
Given the declaration of a C-string variable, where SIZE is a defined constant:
char ourString[SIZE];
The C-string variable ourString has been assigned in code not shown here. For correct C-string variables, the following loop reassigns all positions of ourString the value ‘X’, leaving the length the same as before. Assume this code fragment is embedded in an otherwise complete and correct
int Index = 0;
while (ourString[index] != ‘\0’)
{
outString[index] = ‘X’;
index++;
}
a. Explain how this code can destroy the contents of memory beyond the end of the array.
b. Modify this loop to protect against inadvertently changing memory beyond the end of the array
Want to see the full answer?
Check out a sample textbook solutionChapter 8 Solutions
Problem Solving with C++ (10th Edition)
Additional Engineering Textbook Solutions
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
SURVEY OF OPERATING SYSTEMS
Java: An Introduction to Problem Solving and Programming (8th Edition)
Modern Database Management
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Electric Circuits. (11th Edition)
- In C++arrow_forwardHello, How can I store a whole book in a file in c language? I had some issues because the string is too long. here is the question: For this assignment I have uploaded a large text file that contains the entire book “Robinson Crusoe” byWilliam Defoe as file defoe-robinson-103.txt Consider the code shown below. It uses the fgets function.fgets is used to read (“get”) strings (“s”) from a file (“f”), hence the name “fgets”. It takes the following arguments:1. A pointer to an array large enough to store the string. In our case that is the char array calledbuffer of size MAX.2. The size of the array, in our case the symbolic constant MAX, set equal to 4000 in line 4.3. The file pointer of the file to read from, in our case bkPtr. Each call to fgets returns a string until the newline (“\n”) character or EOF is encountered. The codereads strings from the file defoe-robinson-103.txt in the while loop and prints them to screen.Implement this program and run it.arrow_forwardThis is in c++ 1. Prompt the user to enter a string of their choosing and output the string. 2. Complete the GetNumOfCharacters() function, which returns the number of characters in the user's string. Encouraged to use a for loop. 3. In main(), call the GetNumOfCharacters() function and then output the returned result. 4. Implement the OutputWithoutWhitespace() function. OutputWithoutWhitespace() outputs the string's characters except for whitespace (spaces, tabs). Note: A tab is '\t'. Call the OutputWithoutWhitespace() function in main(). My issue is deleting the spaces. I googled how to delete spaces, but there are terms I haven't learned yet. I'm not sure how to delete the spaces. This is what I have: #include <iostream>#include <string>using namespace std; //Returns the number of characters in usrStrint GetNumOfCharacters(const string usrStr) {int count = 0;unsigned int i;for (i = 0; i < usrStr.size(); ++i) {++count;}return count;} void…arrow_forward
- Please do not use string methods (replace)! Thank you!arrow_forwarduse C++ language for loop to perform this taskarrow_forwardGiven a string on one line and an integer number on a second line, add a copy of the last number of characters of the string to the string's end. Then, output the result. Ex: If the input is: Fuzzy bear 4 the output is: Fuzzy bearbear Note: Using a pre-defined string function, the solution can be just one line of code. #include <iostream>#include <string>using namespace std; int main() { string strVar; int substrLen; getline(cin, strVar); cin >> substrLen; /* Your code goes here */ cout << strVar << endl; return 0;}arrow_forward
- Match the std::string constructor on right to its function on left. Source: https://cplusplus.com/reference/string/string/string/ E string() string (const string& str) string (const char* s) string (size_t n, char c) [Choose] [Choose ] constructs a copy of str. constructs an empty string, with a length of zero characters. copies the null-terminated character sequence (C-string) pointed by s. fills the string with n consecutive copies of character c. [Choose ]arrow_forwardplease code in python Write a function that receives a string and a number (n) as parameters and returns a new string that contains only every nth letter from the given string. print(everyNth('banana', 2)) # should return aaaprint(everyNth('carrot', 3)) # should return rtprint(everyNth('pear', 1)) # should return peararrow_forwardpython only** define the following function: This function accepts a string as its only parameter, and it must return a tuple containing first the length of the string, and then the string itself. Define lengthAndValue with 1 parameter Use def to define lengthAndValue with 1 parameter Use a return statement Within the definition of lengthAndValue with 1 parameter, use return _in at least one place. Do not use any kind of loop Within the definition of lengthAndValue with 1 parameter, do not use any kind of loop.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education