1. Reverse string problem

I have a homework that I've spent almost 1 week but still can not solve. Please help me! I tried my best! I've just started studying Character and String, so this homework could not be complicated. Thanks a lot!!! I'm really frustrated.

Exercise:
Write a program that prints all command line arguments (not including program name) in reverse order, and each in reverse, or “no arguments given” if there are no arguments. For example:
./foo hello world 123
321 dlrow olleh
./foo
no argument given

Hint: _ Functions containing
+ void reverse_string (char* s)
+ int string_length (char* s)
_ Do not use any automatic functions like strlen() or strrev()
_ Main should be about 5-7 lines, reverse should be about 5-7, and length should be about 2-3.

2. Originally Posted by dotrunghai82
I have a homework that I've spent almost 1 week but still can not solve. Please help me! I tried my best! I've just started studying Character and String, so this homework could not be complicated. Thanks a lot!!! I'm really frustrated.
So post your best attempt. And use code tags.

3. No program will ever be written for you, people can give you tips and hints and point out where you are going wrong, but thats about it

4. Code:
```#include <stdio.h>

int main(int argc, char *argv[]){
int i,n;
for(i=argc-1;i>0;i--){
for(n=string_length(argv[i])-1;n>=0;n--){
printf("%c",argv[i][n]);
}
printf(" ");
}
if(argc<2){
printf("no argument given");
}
return 0;
}```
Sorry swgh, but I like programming this kind of things

Code:
```#include <stdio.h>

int main(int argc, char *argv[]){
int i,n;
for(i=argc-1;i>0;i--){
reverse_string(argv[i]);
printf("%s ",argv[i]);
}
if(argc<2){
printf("no argument given");
}
return 0;
}```
Was string_length() necessary, because it is not actually needed , there are two ways to do it, using reverse_string() to reverse the string or using string_length() to read it as reversed, which is a bit more complicated.
If you like the first one more you can just replace strlen() there with string_length(). //Edit: I already replaced it

The first one is a bit better because the arguments remain untouched but the second one reverses the arguments and you cannot use them normally after reversing them.

981st edit:
You needed to make these funcitons yourself?
Code:
```int string_length(char *text){
int i;
for(i=0;text[i]!='\0';i++){}
return i;
}

void reverse_string(char *text){
int i=string_length(text),n,a;
char *temp=malloc(i+1);
for(n=0;text[n]!='\0';n++){
temp[n]=text[n];
}
for(a=0;a<i;a++){
text[a]=temp[i-a-1];
}
free(temp);
return;
}```

5. Well, someone got lucky ... again.

Sorry swgh, but I like programming this kind of things
I'm a newbie too, so I occasionally try the exercises, but that doesn't mean I would give the results to someone to hand in at school. I believe that one learns very little just by looking at some code, if someone putting 13 exclamation marks in a title would bother to do even that.

Anyway, apart from a couple of problems with the code above, the reverse function also seems particularly ugly - making a copy of the whole string just to reverse it!?

6. > Well, someone got lucky ... again.
Didn't they just

Another noob gets through another week of the lecture course in the mistaken belief that they understand what's going on.

We'll see if they come back next week with their empty bowl and "please sir, can I have some more".

7. In the future, use a WAY more descripitive title, include your code, and don't ask for cheap anwers to your homework.

8. It's his own problem. Soon he'll understand he needs to LEARN.

Anyway, apart from a couple of problems with the code above, the reverse function also seems particularly ugly - making a copy of the whole string just to reverse it!?
If you reverse from the source to the source you will end up with reversing hello to olllo and power to rewer. Once I did it in assembly and wondered WT* is wrong?
Anyway, apart from a couple of problems with the code above, the reverse function also seems particularly ugly - making a copy of the whole string just to reverse it!?
I don't think every assignment they get is so interesting that I would try to do it.

9. If you reverse from the source to the source you will end up with reversing hello to olllo and power to rewer. Once I did it in assembly and wondered WT* is wrong?
Maybe you should try again using C. Absolutely, it is possible to reverse a string in place. It's been illustrated here and other places throughout the web countless ways.

And stop doing people's homework and posting it on the board. Do it on your computer, sure, but there is no reason just to give something away.

10. And stop doing people's homework and posting it on the board. Do it on your computer, sure, but there is no reason just to give something away.
I don't even think he has looked this thread after posting it.

11. Topic changed to something meaningful, rather than script kiddie whining with excessive repeated letters.

12. And on that note - closed.