Thread: Anyone with simplier coding than this..

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    Anyone with simplier coding than this..

    Anyone can explain and write out a simplier code of similiar function to the one below...the code is to invert the individual words in a sentence.

    #include<stdio.h>
    #define SP ' '

    char*p(char*t)
    {
    char*s;
    *t&&*t!=SP&&((s=p(t+1))==s)&&putchar(*t);
    return(*t?(*t==SP?t:s):NULL);
    }

    int main(void)
    {
    char*t="i am ok";
    while(t=p(t))
    putchar(*t++);
    }

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Depends on what you mean by simpler.

    Code:
    #include<stdio.h>
    
    void p( const char* t )
    {
        const char *s,*f;
        for(s=t-1;;s=t=f)
        {
            while(*++t&&*t!=' ');
            for(f=t;--t!=s;putchar(*t));
            if(*f) putchar(' ');
            else break;
        }
    }
    
    
    int main(void)
    {
        p("i am ok");
        return 0;
    }
    Also, it differs in that it doesn't check for a pointer to 0 as it's not necissary. That type of checking should be done prior to passing it to the function. If you want it, just add a single if statement to the function.

    EDIT: Kept to obscure variable names for the hell of it
    Last edited by Polymorphic OOP; 02-12-2003 at 05:15 AM.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    > Depends on what you mean by simpler.

    He I've seen this code before.... let me see...
    http://www.cprogramming.com/cboard/s...threadid=33737

    We were just making fun... (see my second post on that thread)

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    Talking

    bad?..looks alrite though...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Reading the last post in the provided thread...

    There is a reason I used recursion: I tend not to just give away the answer to people's homework. I specificly wrote hard to follow code for that exact reason. It provides the solution, but if you can understand what it's doing, then you don't really need to be asking the question.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 07:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 02:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM