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
Suppose a program is running on a distributed-memory multiprocessor. There are 1,000 instructions in the program and 80% of inst
Licemer1 [7]

Answer:

2125 ns.

Explanation:

First of all Execution Time = Number of Instructions * CPI * Clock Cycle

Number of Instructions = 1000

CPI = 0.5

Clock Cycle = 1/clock rate = 1/4GHz = 0.25 * 10-9 s

So Execution Time = 1000 * 0.5 * 0.25 * 10-9

Execution Time = 125 * 10-9 s

Execution Time = 125 ns

Now 20% of the instructions take 10 ns extra time for remote communication.

1 instruction takes 10ns

so 20% of 1000 = 200 instructions will take 200*10 = 2000ns

So Total Execution Time = 125+2000 = 2125 ns.

5 0
3 years ago
To protect your answers after completing an assignment, what should you do? close your browser window. let someone else start wo
zhenek [66]
That should be log out correct....
8 0
3 years ago
6.5 Code Practice: Question 4 Edhesive
tresset_1 [31]

Answer:

Explanation:

print('Enter 10 temperatures')

sum = 0

for i in range(10):

   S = float(input())

   sum = sum + S    

print('The sum is: ',sum)

First, we print a comment asking the ten temperatures, then we declare the variable sum = 0.

We create a for cycle where the user must enter the temperatures, 10 in this case, then we must do the operation, summing the 10 numbers.

In the last step, we print the result with the variable sum.

8 0
2 years ago
When you are finished working with a presentation, you can exit PowerPoint. If there is only one presentation open, you click th
mina [271]

Answer:

TOP RIGHT

Explanation:

To exit the power point when you are finished .There are three buttons on the top right corner

  1. Minimize.
  2. Maximize/Restore down.
  3. Close.

Minimize is to minimize the application to the task bar.

Maximize/Restore down is for the controlling the size of the application on the screen.

Close is for closing the application.

6 0
3 years ago
GIVING BRANLIEST!!!
xz_007 [3.2K]

Answer:

put quotations around the string

Explanation:

the animal name, koala, is of data type string and it should be enclosed in " "

3 0
3 years ago
Other questions:
  • A dropped packet is often referred to as a _____________.
    7·1 answer
  • How can i change ip address <br>​
    7·1 answer
  • In the CPT manual, the digits of the Category III codes are not intended to reflect the placement of the code in the Category I
    5·1 answer
  • You can access cloud files from any computer smart phone or tablet as long as it is connected to the Internet is it true or fals
    8·1 answer
  • You have a chart that shows 100 data points and you've circled the highest value. Which of the following are you using?
    8·1 answer
  • Which device is responsible for converting the main electric supply into usable voltage levels inside a computer?
    12·2 answers
  • Which statement correctly defines the function of a servo
    12·1 answer
  • Write a program to enter 30 integer numbers into an array and display​
    7·2 answers
  • In this assignment you will be building an implementation of the hypothetical machine simulator like the one discussed in chapte
    5·1 answer
  • Can someone pls do a toradora,Mha or princess jellyfish rp I'm open for anyother rp's
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!