Reverse the string.

I'm a beginner in c++ and my new teacher has given me a project on strrev.
I am supposed to write a code with : char *strrev(char *s), which reverses the string order but the last character "0" should stay still. Function returns line s. Say, "Hello1234567890" shoould return "987654321olleH0".
Please help.
Last edited on
Hi, firstly I need more informations.

which reverses the string order but the last character "0" should stay still.

When you say the last character "0" are you talking about the last character stored in the variable OR are you talking about the terminator character '\0' because in c++ there is a character at the end of your char* to delimitate the end of your string but this character is hidden.

The convention that a null, ('\0', numeric value 0) marks the end of data in array of characters is called "C-string", for it is prevalent in C language. The C++ has in its Standard Library std::string, which provides much simpler user interface to "strings" than the C's functions that operate on "C-strings".


There is an another question though (just to be sure):
Does the strrev() modify the array into which the s points to?

If yes, then the return value (a pointer) of the function has the same value as the input parameter s.
If not, then the function must dynamically allocate memory for the C-string that it will return.


In any case, the main purpose of homework is to learn by doing things yourself. It is now your turn to make an effort because we cannot help you with the errors that you have not made.
Background on why I am asking this on this site:
After our teacher who taught everything practically left, a new teacher came who said that we weren't taught correctly and started doing lectures without making us open the computers and gave us homework based on his speeches, only people who understood him were older (30+) in the group that had been doing it before, now he has given this project for the academy exam and i have no idea about the topic. Can you please help me understanding the str topic, unsigned int, strrev, ampersands and char* s. Thank you in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# include <cstring>
# include <cstdio>
# include <utility>

char* strrev(char* str) {
  char* front = str;
  char* back  = str + std::strlen(str) - 1;  

  while (back > front) 
    std::swap(*back--, *front++);

  return str;
}

int main() { 
    char str[] = "Hello World!";
    std::puts(str); 
    std::puts(strrev(str));
}


Output:
Hello World!
!dlroW olleH
Could you please explain every line because i need to somehow keep the last char as it is without changing it's position.
Im not trying to sound like an asshole, this is honest criticism, if you don't understand each line hes writing you should check out some tutorials on pointers and arrays! :) Theres lots of resources out there my friend :)

For a solution to keeping the last letter alive. Maybe put it into a variable called lastLetter, and then add it to the final array?
Last edited on
I know but what i searched up on youtube i only found the basics on how to strrev, I don't know how contact the last char personally, how can i put into a variable in code? Thanks inadvance.
The pointer to the first character is
char* front = str;
The pointer to the last character is
char* back = str + std::strlen(str) - 1;
If you want to avoid reversing the last letter, try messing with the initial value of back.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h> 
#include <iostream> 
#include <cstring> 
#include <utility> 

using namespace std; 

char* strrev(char* str) {
  char* front = str;
  char* back  = str + strlen(str) - 1;  

  while (back > front) 
    swap(*back--, *front++);

  return str;
}

int main() {
    char str[] = "Hello World!"; 
    int lastLetter = strlen(str); 
    puts(str); 
    cout << strrev(str) + 1 << str[11]; 
    
    return 0; 
}


Hacked this in like 10m so dont expect a good grade haha. Enjoy. For some reason I had to physically put a number in the str[11]; because str[lastLetter] isnt working.
Last edited on
char* back = str + strlen(str) - 2;

Why would a function named `strrev' not reverse the whole string? Are you sure it is not the terminating NUL byte which should not be moved?

For some reason I had to physically put a number in the str[11]; because str[lastLetter] isnt working.

For a string of length n, the last character is at offset n - 1, i.e., the last character is str[length - 1]
Last edited on
Thank you for the help, what about the part where the answer needs to be displayed in the first line? Should i use "strcpy(input, output)" ?
utoob is one of many resources. a lot of coding help will be in text, not video. that is not bad; you can read 30 pages in the time some guy can read 3 pages aloud, so often the text is more efficient.

strcpy is of the format
strcpy(destination, source);

if you want the result in output, your statement is wrong, but I can't tell if your variable names are mismatched to the function or just in the function in the wrong order. You can google strcpy to see examples.

make friends with sprintf too; its how you put variables into strings.

eg
int r = 1234;
sprintf(output, "the answer is %i units \n", r);
output contains:
"the answer is 1234 units" (+end of line)

this is identical to strcpy:
sprintf(output, "%s", input);
and this is identical to strcat:
sprintf(output, "%s%s", string1, string2);
Last edited on
Topic archived. No new replies allowed.