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
Why are salaried employees often excluded from overtime pay
lyudmila [28]

The reason why salaried employees often excluded from overtime pay because  if they are able to make a given amount or carry out a specific duties that are not recognized, they are said to be not eligible for overtime pay.

<h3>Do salaried employees get paid overtime?</h3>

A salaried employee is one that gets paid due to different kinds of work. They can be paid overtime unless there is a reason that they do not meet the test for exempt status as stated in the  federal and state laws.

Note that they are not eligible if they are specifically exempted from any form of overtime by the provisions that were given in the California Labor Code or the Industrial Welfare Commission Wage Orders that is known to be regulating wages, hours as well as working time.

Hence, The reason why salaried employees often excluded from overtime pay because  if they are able to make a given amount or carry out a specific duties that are not recognized, they are said to be not eligible for overtime pay.

Learn more about overtime pay from

brainly.com/question/19022439

#SPJ1

5 0
1 year ago
How does Virtual Reality help to make work experiences more inclusive?
FrozenT [24]

Answer:

It makes your brain work better and it's also fun.

Explanation:

The brain does not like just one thing it wants to have more experience.

7 0
2 years ago
What authentication protocol is ticket-based and is used by windows computers that are members of an active directory domain?
Temka [501]
Your IP address, also known as Internet Protocol
7 0
3 years ago
Why might people feel differently about their digital lives?
denpristay [2]
Same reasons as why people feel differently toward real life. It depends on your activities on Internet, and the kind of environment you spent in it, mainly how other users interacted you.
7 0
3 years ago
Blurring in a photograph can be due to _____ or ______.
scoray [572]
Do you have options? In my personal opinion, however, blur could be caused by motion blur, or focusing on the wrong thing, Hope this helps any!
4 0
3 years ago
Read 2 more answers
Other questions:
  • To arrange data in alphabetical order quickly, you can _____ the data.
    7·2 answers
  • When programming in Scratch, to make our Sprite walk across the background, in which category would we find the programming bloc
    7·1 answer
  • Read the scenario below and then answer the
    14·1 answer
  • IT investments can lead to developing IT capabilities and dynamic IT competencies, which can lead to achieving the following six
    5·1 answer
  • In the 1880’s advancements in technology and processes made photography available to the general public. Who is considered the m
    12·1 answer
  • In paragraph form, explain and describe at least three common situations in which you would yield the right-of-way.
    5·1 answer
  • in java Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, o
    13·1 answer
  • Which of these is an example of input?
    6·1 answer
  • Use a while loop to output the even number from 100 to 147? This is python
    10·1 answer
  • DRAW A FLOWCHART THAT WILL ASK THE USER TO ENTER AND DISPLAY THEIR PERSONAL DETAILS SUCH AS NAME AND AGE.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!