codes

This is a discussion on codes within the C Programming forums, part of the General Programming Boards category; Originally Posted by CproG100 yes, sure u added #include <stdio.h> i got lost in your explanation a little bit... so ...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by CproG100 View Post
    yes, sure u added #include <stdio.h>
    i got lost in your explanation a little bit... so can write str[i]= 'y'+ word ?
    and about the space code... how do i reverse the process? i mean to move each space chars left ?
    If string[] is a single row, then string[i], is just a single char. You can't put more than one char into any one char array element - that's all any of them can hold.

    Plus signs don't work with char or string joining, in C. You can use + with a single char: 'a' + 1 equals 'b', but you can't make "abc" with a+b+c.

  2. #17
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    so how do i write it ?
    and 'ab' is not 'a'+1....

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Use a for loop, and work it like I described it in reply #10 of your thread.

    Post up a specific example that you need help with, and a program where you have tried to do it.

    Because the thing is, you have to practice, practice, practice. I can describe swimming to you, but you won't know how to swim, until you get into the water and get some practice in. That's how programming is. You learn a lot by doing it.

    FYI: Chimps can't swim - they drown, and are afraid of deep water because of it. Only one bright one happened to learn how to in a zoo, (that we know of).

    Right now, I'm not clear, on what you're not clear about. I'll know in a flash, as soon as I see your code.

  4. #19
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    o.k., about what i asked at the begging in the attached file.. i did it!! it's working! the thing is i don't know how to reverse the process, meanning- move each space one char to left.
    here is my code for moving each space one char right.
    and also, about the other quistion about adding 'ay'... it's not going well.. can someone help?
    Code:
     
    Code:
     void SpaceCodeEncode(char str[])
    {
    	
    int i;
    	
    for ( i = 0 ; str[i] != '\0'; i++)
    	{
    		
    if (str[i] == ' ')
    		{
    			str[i] = str[i + 1];
    			str[i + 1] = ' '; 
     
    			i++;
    		}
    	}
    }
    


  5. #20
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    / ///

  6. #21
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    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.
    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"

  7. #22
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    i don't really understand....

  8. #23
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Give an example of what the reverse method should do,the one that moves one space one char left..
    Also post the attempt you have for the turkish irish

  9. #24
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    i did everything the same but chainging str[i + 1] to str[i-1]

  10. #25
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by CproG100 View Post
    i did everything the same but chainging str[i + 1] to str[i-1]
    Sorry ,i do not understand....the same with the reverse method?Also please give an example

  11. #26
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    Code:
    void SpaceCodeEncode(char str[])
    {    int i;
    for ( i = 0 ; str[i] != '\0'; i++)
        {        if (str[i] == ' ')
    
    
            {            str[i] = str[i - 1];           
     
    str[i -1 1] = ' ';  
                i++;     
    }
    
    }}
    

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Eh, CproG100, you seriously need to format your code properly. Also, just post the plain code (no special markup) in [code][/code] bbcode tags:
    Code:
    void SpaceCodeEncode(char str[])
    {
        int i;
        for (i = 0 ; str[i] != '\0'; i++)
        {
            if (str[i] == ' ')
            {
                str[i] = str[i - 1];
                str[i -1 1] = ' ';
                i++;
            }
        }
    }
    Anyway, there's what looks like a syntax error: str[i -1 1]. You probably meant: str[i - 1]. Now, thanks to the guarantee that there will be no space at the start of the sentence, you are okay, but this is where you have a potential array out of bounds error: if i == 0, then you are trying to access and write to str[-1], which presumably does not exist. As such, to take advantage of the guarantee, you should begin with i = 1, not i = 0.

    Then, after the "swap", you inexplicably increment i again, which means that the loop will skip the next character. I don't see a guarantee that says that there will not be consecutive spaces, so this looks wrong to me.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Yes it looks very wrong. To move items along by one in the other direction requires reversing the loop direction. Otherwise you just copy one item over top of all the rest.
    But you should really fix your other one first, with regards to the missing nul-termination.

    That was the last time you get away with awfully formatted code as far as I'm concerned. Next time it isn't tidly formatted like how laserlight shows, you will find yourself without help.
    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"

  14. #29
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    i'm not sure what you meant about what i need to do with the spaces....
    can you explain more about why str[-1] ? i don't get why...
    thanks.

  15. #30
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    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.

Page 2 of 3 FirstFirst 123 LastLast
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, 09: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, 03:06 PM

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