Question

In: Computer Science

Write a function void reverse(char * s) that reverses the string passed as an argument. Your...

Write a function 
               void reverse(char * s)
that reverses the string passed as an argument. Your code should use pointer arithmetic (it may increment and decrement pointers, but it may not use array indexing). 
 
Here is a piece of code that shows the behavior of reverse:
char buf[100];
strcpy(buf, “hello”);
reverse(buf);
printf(“%s\n”, buf);  // output should be olleh
 
 

Solutions

Expert Solution

#include <stdio.h>
#include <string.h>

void reverse(char * s) {
   char temp;
   char *last = s;
   while(*last) {
       last++;
   }
   last--;
   char *first = s;
   while(first < last) {
       temp = *first;
       *first = *last;
       *last = temp;
       first++;
       last--;
   }
}

int main() {
   char buf[100];
   strcpy(buf, "hello");
   reverse(buf);
   printf("%s\n", buf); // output should be olleh
   return 0;
}

if this Answer is helpful to you please give positive rating.

if any doubts please provide a comment i will try to clarify your doubts


Related Solutions

Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write, specify and prove the function reverse that reverses an array in place. Take care of...
Write, specify and prove the function reverse that reverses an array in place. Take care of the unmodified part of the array at some iteration of the loop. Assume that the swap function is already proved. Note: Prototype is as below. [7 M] [CO2] void swap(int* a, int* b); void reverse(int* array, size_t len){ }
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack s using a Queue. Test your method using some example stacks. In java
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary...
There is a C function decodeMorse(const String & string, char message[]). This function examines the binary string and iteratively constructs a decimal value (val) and width of each binary pattern (separated by spaces), until a space or a null character ('\0') is encountered in the string. Once a space or a null character is found, this function should call the assembly code (decode_morse()) to obtain the corresponding ASCII value, for the current val and width, and place the ASCII value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT