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
During spring in Colorado, ice on the mountain tops melts into water. Some of this water gets trapped in cracks in the mountain
zlopas [31]
Physical weather. It isn't changing the the chemical or biological make up.
3 0
3 years ago
Read 3 more answers
How to Ctrl + shift + F4 but in a HP laptop?​
ludmilkaskok [199]

Answer:

Hit the X Button Located on the top right corner or hit Control, Alt, Delete

Explanation:

Hitting the X button will make you exit the whole cite, or hitting Control, Alt, Delet, will make you go to the task bar, in which you can go to the bottom right of that screen and it should say end task

3 0
3 years ago
Read 2 more answers
What can you look for on a website to indicate that the information it presents is reliable?
dybincka [34]

There are several things you can look for on a website to help you figure out if the information is reliable. The first thing you should evaluate is the audience that the website is intended for. Is it intended for academics? School children? The general public?


The next thing you should look at is the author of the website. Is the author identified? Is the author an expert in their field? Can you establish the author's credibility? Is the author affiliated to an academic institution or credible organisation?


Look at the accuracy of the website. Check for spelling errors, proper grammar, and well-written text. Are there any sources cited? Are those sources credible?


You should also check to see when the information was published. Is the information up to date? Are all of the links up to date and functioning?


There is one last thing you can look at, and this is the domain of the website. Domains like .edu and .gov are more credible than .com or .net domains.

5 0
3 years ago
Productivity, rework, and technology impact are examples of which kind of software metric?
fgiga [73]

The correct answer should be process type of software metric. The process metrics are used to help in strategic decision making. The processes such as work products delivery, expended human hours and conformance to quality and schedule are all metrics in the process domain.

6 0
3 years ago
Omar sends a PDF file to his teacher. He then prepares another copy of the presentation that includes only the slides he wants t
maria [59]

Answer:

D. Custom Slide Show

Explanation:

Correct on edg

7 0
3 years ago
Other questions:
  • An outline is most like which of the following?
    7·1 answer
  • Consider a satellite orbiting the earth. Its position above the earth is specified in polar coordinates. Find a model-view matri
    12·1 answer
  • On a router configured to use RIP, the number of routers a packet must travel between before it reaches its destination is calle
    7·1 answer
  • A computer has been stored, uncovered, in a dusty closet for several months. Why might this situation cause the operating system
    13·1 answer
  • Subscribe to (Thicc sickqueen) and like 2 vids and the claim your prize
    13·1 answer
  • Classify the various scenarios in a web development team that is creating a website for a client according to the priority level
    9·1 answer
  • Zachary drinks 2 cups of milk per day. He buys 6 quarts of milk. How many days will his 6 quarts of milk last?
    12·1 answer
  • Dominic list his camera for sale on an online auction site Chloe is the highest bitter and purchase as the camera how does the a
    14·1 answer
  • I dont know how to put the negative sigh on my computer
    5·2 answers
  • Identifiy the impact of new technology for your organizations <br>​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!