1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Zina [86]
3 years ago
6

Your team will write a function that reverses a C-string, to practice using pointers. You must use pointers and C-strings for th

is lab, not string objects. Your program should ask for the user's input, as "Enter the original string: " Then, it should print out the reversed string to the console.

Computers and Technology
1 answer:
Nutka1998 [239]3 years ago
3 0

Answer:

Here is the C++ program:

#include <iostream> //to use input output functions

#include<string.h> // to manipulate strings

using namespace std; //to identify objects like cin cout

void ReverseString(char* original_str){ // function that reverses a C-string

       char* ptr1 = original_str;  // pointer that points to the start of C-string

       char* ptr2 = original_str; // pointer that points to the start of C-string initially

       for (int i = 0; i < strlen(original_str)- 1; i++) {//iterates through the length of the C-string

           ptr2++;} //moves ptr2 to the end of the C-string

       while(ptr1 < ptr2) {// swaps each character of C-string using pointers

               char temp = *ptr1; //declares a temp variable to hold character that *ptr1 points to

               *ptr1 = *ptr2;

               *ptr2 = temp;

               ptr1++; //increments ptr1 moving it forward

               ptr2--;   }} //decrements ptr2 moving it backwards

int main(){ //start of main() function

       char input_str[]="";  /// declare a C-string

       cout<<"Enter original string: "; //prompts user to enter a string

       cin>>input_str; // reads that C-string from user

       ReverseString(input_str); //calls function to revserse the input string

       cout<<"\nReversed string: "<<input_str;} // displays the reversed string

Explanation:

The program has a function ReverseString that takes a C-style string as parameter and reverses the string using pointers.

The ptr1 and ptr2 are pointers that initially point to the beginning of original string.

for (int i = 0; i < strlen(original_str)- 1; i++) this statement has a for loop that iterates through the original string and moves the ptr2 to point at the end or last character of the C-string. strlen() method is used to get the length of the C-string. At each iteration ptr2 moves one time forward until the last character of the C-string is reached.

while(ptr1 < ptr2) statement has a while loop that works as follows:

checks if ptr1 is less than ptr2. This evaluate to true because the index of first character that ptr1 points to is 0 and last character is 5 so 0<5. Hence the statements inside while loop execute which swaps the character of C-string from beginning and end index using ptr1 and ptr2. a temp variable is declated to hold character that *ptr1 points to.

For example

original_string = "xyz" Then temp = *ptr1; stores the character that *ptr1 points to i.e. 'x'

*ptr1 = *ptr2; This statement assigns the character that *ptr2 points to the *ptr1. Basically these pointers are used as indices to the C-string. So this means that the *ptr1 now stores the last character of the C-string that is pointed to by *ptr2. So *ptr1 = 'z'

*ptr2 = temp; This statement assigns the character that temp stores to the *ptr2. We know that temp contains the first character i.e 'x'. So *ptr2 = 'x'      

ptr1++; The ptr1 is incremented to 1. This means it moves one position ahead and now points to the second character of the C-string.

ptr2--; The ptr2 is decremented to 1. This means it moves one position backwards and now points to the second character of the C-string.

Now the while loop breaks because ptr1 < ptr2 condition evaluates to false.

So the original string is reversed using pointers. Hence the output is:

zyx

screenshot of the program along with its output is attached.

You might be interested in
"The principle of ______________ states that programs tend to access data and instructions nearby recently used data and instruc
maria [59]

Answer:

<em>Locality of reference.</em>

Explanation:

In computing, The principle of locality of reference is the probability of a processor to repeatedly access the same set of memory locations within a small time interval. This reference of locality can be spatially based (repetitive usage of data within the same location on the computer memory), or temporally based (repetitive usage of a particular data or resources within a shot time interval). The ability of some computing system to perform this action increases their predictability, and efficiency of memory hierarchy use, among other abilities.

4 0
3 years ago
ImmuneEarth, a research and development company, has "eradicate all diseases" as its motto. It has numerous departments that res
anastassius [24]

Answer:

Grid computing.

Explanation:

Grid computing is a study utilizing Immune Earth to test AIDS treatment because the following are the reasons that describe the answer is true about the scenario.

  • Grid computing seems to be a scattered network with a vast amount for systems linked to handle a difficult issue.
  • Systems can be linked straightly or through organized systems.

Other options are incorrect.

  • Green computing is not the correct answer according to the scenario because the usage of systems and its energy is socially safe and economically sustainable.
  • Trusted Computing is not the correct answer according to the scenario because it helps a part of information to show which os as well as program to be required to access this.
  • Edge computing is not the correct answer according to the scenario because it optimizes web apps as well as software programs while getting computation back to the information base.
4 0
3 years ago
A variable that can have values only in the range 0 to 65535 is a :
Thepotemich [5.8K]

Answer:

a.

Explanation:

Two bytes have 2 times 8 bits is 16 bits.

Max value that can be expressed is 2¹⁶-1 = 65535

6 0
3 years ago
Which act requires enterprises to guard protected health information and implement policies and procedures to safeguard it?
bogdanovich [222]

Answer:

D. HIPAA (Health Insurance Portability and Accountability Act)

Explanation:

HIPAA which stands for Health Insurance Portability and Accountability Act, is an act designed by the US government in 1996 to guard patients' confidential health information and also implement policies and procedures to safeguard it. The act contains the required information and instruction for handling patients' medical health information. The law however gives patients (or their representatives) the right to access these information which as a matter of fact must be made available within 30 days from the day of request.

PS: Not sure why the option D in the question contains ' FTF '. I have taken it to be a typo because without it, the option is in itself complete.

7 0
3 years ago
A Program is an Algorithm – a set of directions for the computer to follow.<br><br> True<br> False
d1i1m1o1n [39]

Answer:

True

Explanation:

5 0
2 years ago
Other questions:
  • Which of the following situations would not require knowledge of networking?
    6·1 answer
  • To make sure that you do not get too tired when typing for long periods, how often should you get up and stretch? Every 15 minut
    9·1 answer
  • If you were to create a new app for a smartphone or tablet that does not already exist, what would you create?
    10·1 answer
  • What does Pentium means?:/
    7·2 answers
  • Match the keyboard shortcuts to the function they perform
    10·1 answer
  • Which component of service-oriented DSS can be defined as data that describes the meaning and structure of business data, as wel
    9·1 answer
  • Out of a list of the values 8, 3, 30, 5, 15, and 6, what result would the MIN function return?
    9·2 answers
  • Jane is creating a slide that will have a large heading and number of bullet points below it. What slide format should she use?
    12·1 answer
  • What is theory of knowledge?​
    13·1 answer
  • Complete the following sentence by choosing the best answer from the options listed below.
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!