Thread: reverse string

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    reverse string

    Code:
    #include<stdio.h>
    main()
    {
          char buffer[50];
          char *reversed;
          gets(buffer);
          reversed=malloc(strlen(buffer)+1);
          int g,f;
          reversed[strlen(buffer)+1]='\0';
          for(g=strlen(buffer)-1,f=0;g>=0;g--,f++)
          {
              reversed[f]=buffer[g];
              }
              printf("%s",reversed);
    }
    &&&&&&&&&&
    Code:
    #include<stdio.h>
    main()
    {
          char buffer[50];
          char *reversed;
          gets(buffer);
          reversed=malloc(strlen(buffer)+1);
          int g,f;
          reversed[strlen(buffer)+1]='\0';
          for(g=strlen(buffer),f=0;g>=0;g--,f++)
          {
              reversed[f]=buffer[g];
              }
              printf("%s",reversed);
    }
    sorry for using gets and intendations and all this but i have no time really
    any way the first one works because i add -1 but the second one doesn't ???
    why is that ?
    thanks in advance

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Your indentation always sucks. What is really so pressing in your life that you can't take the time to indent code properly as you write it?

    And seriously, does it really take much to use fgets() instead of gets()?

    Anyway, strlen(buffer) will contain the length of the string. The length of the string, when used as in index, is one past what you want since it'll hold the index of the '\0'. This is why subtracting 1 works.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    in the statement
    for(g=strlen(buffer),f=0;g>=0;g--,f++)

    problem is at "g>=0".
    If string length is of 6 then it will try to traverese the string 7 times. At seventh place it will find nothing and may result ambiguos.

    Simply understand
    for(i=0; i<6; i++) will loop only 6 times while
    for(i=0; i<=6; i++) will loop 7 times.

    I think now u hav got what u r doing wrong.
    S_ccess is waiting for u. Go Ahead, put u there.

  4. #4
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    for your informations i am using notpad and if you think my indentation sucks you see this machine :@
    "What is really so pressing in your life that you can't take the time to indent code properly as you write it?"
    believe me allot of crap
    any way you mean when i use the strlen as index it contains the index of '\0' also
    so by this i copied it to the word[] when i didn't subtracted 1
    thanks any way and sorry for bothering you with my indentation

  5. #5
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by maven View Post
    in the statement
    for(g=strlen(buffer),f=0;g>=0;g--,f++)

    problem is at "g>=0".
    If string length is of 6 then it will try to traverese the string 7 times. At seventh place it will find nothing and may result ambiguos.

    Simply understand
    for(i=0; i<6; i++) will loop only 6 times while
    for(i=0; i<=6; i++) will loop 7 times.

    I think now u hav got what u r doing wrong.
    no but thanks
    if i did this
    Code:
    for(g=strlen(buffer),f=0;g>0;g--,f++)
    its not gonna copy the first letter

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Okay.
    i Got it.

    Lets say string is Thread. That means string length is 6. But in array actually at 5th(0,1,2,3,4,5) location last letter 'd' is stored. So it should start reversing from 5 th position.
    S_ccess is waiting for u. Go Ahead, put u there.

  7. #7
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    right but look at this
    Code:
    ]for(x=5;x>0;x++)
    {
    printf("&#37;d",x);
    }
    =[5,4,3,2,1]
    it's not gonna print 0
    but
    Code:
    for(x=0;x>=0;x++)
    {
    printf("%d",x);
    }
    =[5,4,3,2,1,0]

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    for(x=0;x>=0;x++)
    {
    printf("&#37;d",x);
    }
    =[5,4,3,2,1,0]
    I dobt it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    reversed=malloc(strlen(buffer)+1);
    int g,f;
    reversed[strlen(buffer)+1]='\0';
    This writes to memory you don't control. If strlen(buffer) returns 10 (for example) then you are correctly allocating 11 spaces to take care of the extra null that needs to be in the reversed string. This gives you a range of valid indexes from 0 to 10 inclusive (11 characters total) to use when storing stuff into reversed. You would however then be trying to store a null in the 11th position which is 1 more than you should.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You should only need one number for a for loop and the string length here.

    count to the length of the string. The backwards versions position will be the length of the string minus the position in the loop -1.

  11. #11
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    thanks all for your advices
    sorry for my bad indentation

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    Code:
       for(int i =0;i>strlen(string);i++)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM