Thread: codes

  1. #31
    Registered User
    Join Date
    Sep 2006
    Before you work on reversing the loop, check that you have the first one, really correct:

    1) Did you add the end of string char onto the end of the string, yet? (as mentioned by iMalc). That should go right after the for loop finishes.

    2) Did you check out Laserlight's concern about the i++ when there is an empty space? Your code should fail if there are two spaces in a row. (and is it necessary to increment the i when you reach a space? Why not let the loop handle incrementing the i?).

    Reversing the loop would mean starting from the end, and decrementing the loop counter (i-- instead of i++), if you want to use a similar logic.

    We have no idea what is done "deliberately" and what is not. You can see hundreds of examples of how to post right, and how to format your code correctly here. Follow them, and please - forget being "thin skinned", here. Think of "water off a duck's back", as much as you can. The best excuse here, is no excuse at all. Like Nike says: "Just do it". You'll learn far more than you will in an argument -- unless it's an argument about code, of course!

  2. #32
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    and about the code, why on earth are you so aggressive?? it's not like i did it Deliberately.. so don't threat.

    Do you really think "Post reasonable code or I will not continue to help." is a threat? That greatly reduces the meaning of the word "threat".


    Anyway, it works like this; the people don't want to waste their time helping someone who continually posts code that is difficult to reason about. We can't help with code we can't reason about.

    This isn't about threats. It is about you giving people the perception that you are wasting their time.


  3. #33
    Registered User
    Join Date
    May 2012
    Quote Originally Posted by iMalc View Post
    That code you've posted fails to nul-terminate the string afterwards. It will work provided that there are nuls in the rest of the buffer, but this code should not rely on that.
    The only problem I can see with that code is the case were a space is before the null character:
    #include <stdio.h>
    int main(void)
        char string[] = "a b c \0xxxx";
        int i;
        printf(">%s<\n", string);
        for (i = 0; string[i] != '\0'; i++)
            printf("%d - %c\n", i, string[i]);
            if (string[i] == ' ')
                string[i] = string[i + 1];
                string[i + 1] = ' '; 
                printf("%d - %c\n", i, string[i]);
        printf("\n>%s<\n", string);
        return 0;
    Then i will be out of bounds.
    But according to the assignment in post #1 the input has to be cleaned (removing leading and trailing spaces and redundant spaces in between). Thus adding
    string = clean_input(string);
    at the beginning of the function makes it save.

    Bye, Andreas

  4. #34
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Quote Originally Posted by CproG100 View Post
    how can i reverse the loop ? i'm just a begginer so this things are new to me.
    and about the code, why on earth are you so aggressive?? it's not like i did it Deliberately.. so don't threat.
    To reverse the loop, think about where is starts and where it ends. Then instead of going from a to b, it will need to go from b to a. If it went up each time before, then now it will go down each time. Sorry, I'm trying to do this through hinting.

    Not trying to be aggressive there. Call it shock treatment, though a mild version at that. We simply want you to realise the importance of it, and make things easy on everyone. You'd be surprised how often beginners cant find a bug purely because of poor code formatting. They're shooting themselves in the foot; Don't be one of them.

    True yeah there is only a problem when the function ends with a space, but it's so much easier to fix that than wasting time tracking down the problem later because you introduced a bug elsewhere that stopped the space being trimmed off, or it was used from somewhere where spaces were not trimmed.
    Last edited by iMalc; 09-05-2012 at 01:37 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c codes need help,thanks
    By hth373737 in forum C Programming
    Replies: 21
    Last Post: 11-23-2008, 10:45 PM
  2. Need some C++ codes...
    By blah569 in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2005, 02:21 PM
  3. zip codes
    By missny in forum C Programming
    Replies: 10
    Last Post: 09-16-2005, 02:39 AM
  4. VK codes
    By Hunter2 in forum Windows Programming
    Replies: 10
    Last Post: 08-24-2002, 09:58 AM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 04:06 PM