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".
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.
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?
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.
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
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.
int r = 1234;
sprintf(output, "the answer is %i units \n", r);
"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);