Function of void print and its capacity in this code.

Hello, i was given a project to do on c++, i was supposed to write a cpp code that would reverse a *char element(word) leaving the last letter as it is, say, "12345" would be "43215".
Can you please explain what the use of the void print(char*str) function and its contains. I need to explain this code to my teacher but i stuttered on this part. Thanks inadvance.






#include <stdio.h>
#include <iostream>
#include <cstring>
#include <utility>
#include <string>

using namespace std;

char* Strrev(char* str) {
char* front = str;
char* back = str + strlen(str) - 2;
while (back > front)
swap(*back--, *front++);
return str;
}
void print(char*str)
{
for (int i = 0; i < strlen(str); i++)
{
cout << *(str + i);
}
cout << endl;
}
int main() {
char str[50];
cin >> str;
char *str2;
print(str);
str2 = Strrev(str);
print(str2);
}
You need to explain to your teacher. That both shows what you understand and helps you to understand. We cannot set knowledge into your head, but we can give you some questions to ponder.


I presume that you have not run the program to see what happens?

Where is that function used? Does that in any way correlate with what you see the program to do?

1
2
3
4
5
6
7
8
9
void print(char*str)
{
  for (int i = 0; i < strlen(str); i++)
  {
    cout << *(str + i);
  }

  cout << endl;
}

What is in the function?

Is there a loop? If yes, how many times does it repeat? What happens in the body of the loop?

What else is there in the function?
All i ask for is to explain in detail what every line is responsible for, i have written half of this code and gotten help from somebody else but from his explanation he told me that it enables to print in the first line. I have ran this program and it works perfectly fine. User inputs a word, it returns the inputted word and then the reversed word but the last letter stays the same, 'hello12345' to '4321olleh5'.
So you got someone else to do your work for you, and now you want us to help you lie to your teacher and make it look like you wrote it.

Have I understood that right?
"All you ask" will ruin your life.


I think you should start by reading all of the tutorial: http://www.cplusplus.com/doc/tutorial/
Then you can read http://www.cplusplus.com/reference/cstring/strlen/
void print(char*str)

do you know what that does?


for (int i = 0; i < strlen(str); i++)

Are you familiar with for loops? If not, look it up.
Do you know what strlen does? If not, look it up.

cout << *(str + i);

Are you familiar with pointer arithmetic? If not, look it up.
What is this statement doing when i is 0? When i is 1?

cout << endl;

Surely you know what this is. If not, you will not do well in this class, but look up endl anyway.



When you learn what each of the steps does, answer this question. How does your function differ from:

1
2
3
4
void print(char*str)
{
  cout << str << endl;
}


When you can answer this question, you will be able to explain your (or "somebody"'s) code to your teacher.
Alright, let me get this straight, i haven been given this project to do, i wrote what i could and went to the academy i learn in and asked for an extra lesson, a new teacher was appointed for me and with his help we finished the project, he helped me by putting the for loop into the void print function which i didn't know anything about (our teacher didn't teach us anything about it). I have understood every single line in this cpp code except the line where the function voidprint(char*str) is declared.
no need to come at me as if i cheat or anything, i deliberately didn't include background on the project to get the solution as short and quick as possible.
Topic archived. No new replies allowed.