Reverse string problem

This is a discussion on Reverse string problem within the C Programming forums, part of the General Programming Boards category; I have a homework that I've spent almost 1 week but still can not solve. Please help me! I tried ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    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. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote 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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    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. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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

    Oh, and with your functions:
    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;
    }
    Last edited by maxorator; 10-30-2006 at 07:15 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > 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".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    In the future, use a WAY more descripitive title, include your code, and don't ask for cheap anwers to your homework.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    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. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Topic changed to something meaningful, rather than script kiddie whining with excessive repeated letters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    And on that note - closed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Problem with my string reverse code
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-24-2005, 09:22 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21