Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Need help fast. I sucks ass at programming.

Name: Anonymous 2009-05-18 22:21

int main()
{
    char s[50], s2[50];
    int i,j;
    printf("type string");
    gets(s);
    for (i=0; s[i]!= '\0'; i++);
    for (j=0; j<i; j++){
        s2[j]=s[i-j];
    }
    puts(s2);

}
 It was suppose to "invert" the string. If I type "abcd" it was supposed to come out as "dcba". I fail at life.
How can I fix this?

Name: Anonymous 2009-05-18 22:29

How can I fix this?
Exit life.

Name: Cocks 2009-05-18 22:30

how is it failing?

Name: Suigintou !c/Alice/AE 2009-05-18 22:32

Been a while since I lurked /prog/, nice to see the userbase is still as dumb as shit.

Name: Anonymous 2009-05-18 22:34

/g/ already helped. Nevermind this post.

Name: Anonymous 2009-05-18 22:35

    for (j=0; j<i; j++){
        s2[j]=s[i-j];
    }
the variable i will be the length of the first string. the largest value of j will be i minus one, leaving your final string as ?dbca???? since it starts at too high an index. make the core of the loop s2[j]=s[i-j-1]; and it should work.

Name: Anonymous 2009-05-18 22:36

Insert i--; between the two fors.

Name: Anonymous 2009-05-18 22:36

>>6
Thank you.

Name: Anonymous 2009-05-18 22:38

>>4
nobody gives a fuck gb2/jp/

Name: Anonymous 2009-05-18 22:46

>>7
--i;

Name: Anonymous 2009-05-19 1:24

>>10
in this case, --i is the same as i-- (it's all by itself)

Name: Anonymous 2009-05-19 2:23

>>11
Not quite -- though the compiler may optimize the temporary copy taken before the decrement.

Name: Anonymous 2009-05-19 2:25

>>11
unless you're stupid enough to compile it with a sepples compiler, in which case you have OH SHIT OUT OF MEMORY problems.

Name: Anonymous 2009-05-19 2:57

How can I fix this?
Actually return a value from main as specified by the declaration.
Don't use gets().
Don't use C.
Dont use puts().
Don't take any more computer science classes.
Don't continue breathing.

Name: Anonymous 2009-05-19 9:49

>>14

Good advice but it doesn't actually fix what's been done.  He should ask the USENET Oracle for a penance.

Name: Anonymous 2009-05-19 9:51

>>12
There's no need to make a temporary before the decrement in i--, as the decrement is peformed after the rest of the expression.

Name: Anonymous 2009-05-19 9:51

>>14
Don't use C.
GET OUT

Name: Anonymous 2009-05-19 12:46

main = liftM reverse getLine >>= putStrLn

Name: Anonymous 2009-05-19 13:46

Try VIOLENT RAPE.

Name: Anonymous 2009-05-19 14:13

>>18
main = reverse <$> getLine >>= putStrLn

Name: slightly easier to read 2009-05-19 14:24

>>20
main = putStrLn =<< reverse <$> getLine

Name: Anonymous 2009-05-19 16:58

$ rev

Name: Anonymous 2009-05-19 17:32

there is my rev function from problem 55 from project euler


void rev(char *s, char *sr) {
  char *p,*pr = sr + strlen(s);
  *pr-- = 0;
  for(p=s;*p;p++,pr--)
    *pr = *p;
}

Name: Anonymous 2009-05-19 18:04

>>23
there is my ver function from >>24 from /prog/

void ver(char *s, char *sr)
{
    char *p = s, *pr = sr + strlen(s);
    *pr-- = 0;
    while (*pr-- = *p++)
        ;
}

Name: Anonymous 2009-05-19 19:54

>>24
expert programmer

Name: Anonymous 2009-05-20 0:19

>>24
+1 (Obfuscating)

Name: Anonymous 2009-05-20 0:20

>>24
what the fuck is that

Name: Anonymous 2009-05-20 0:28

>>25
hardly, some of his variable names exceeded one character. an obvious beginner mistake

Name: Anonymous 2009-05-20 0:34

>>24
Nice. Now I want to see it written in direct ENTERPRISE JAVA BYTECODE.

Name: Anonymous 2009-05-20 6:42

>>28
no, his variables were derived from >>23, who is only a [b][i][o]semi-expert programmer[/u][/i][/b]

Name: Anonymous 2009-05-20 6:57

>>30
Look who's talking

Name: Anonymous 2009-05-20 7:10

>>24
What are p and pr for? They are completely unnecessary, making the code is highly bloated and redundant. There's also a fatal mistake: it'll write a \0 before the output string, very probably outside the provided destination buffer.

The sad thing is that it's still the best piece of code in this thread, by a few miles.

Name: Anonymous 2009-05-20 7:48

void evr(char *s, char *sr)
{
    sr += strlen(s);
    *sr-- = 0;
    while (*s)
        *sr-- = *s++;
}

Name: Whitespaces suck 2009-05-20 8:03

void evr(char*s,char*sr){sr+=strlen(s);*sr-- =0;while(*s)*sr-- =*s++;}

Name: Anonymous 2009-05-20 8:34

>>34
OMG OPTIMIZED

Name: Multi-letter names suck 2009-05-20 13:43

void e(char*s,char*r){r+=strlen(s);*r--=0;while(*s)*r--=*s++;}

Name: Needs more !.c.yXP2vFk 2009-05-20 14:37

>>36
#define v void
#define c char
#define t strlen
#define w while
v e(c*s,c*r){r+=t(s);*r--=0;w(*s)*r--=*s++;}

Name: Anonymous 2009-05-20 15:14

>>37
no

That makes it longer. In order to successfully use #define to optimize it, you need to make sure the #define replaces at the very least, 11 (#define + 2 spaces + replace character + use of replace character for each instance) + replaced text length characters.

For example, with void, you would need to make sure it replaces no less than 15 characters. That's just to break even. The only things even close to being #define-compatible for optimization in that particular code excerpt are *r--= and char * and even those aren't close to a gain.

It's over; you've lost.

Name: Anonymous 2009-05-20 15:30

>>38
#define is preprocessor-code.
We need that anyway for the string.h, so just add those lines to the string.h, but call it s.h to save even more space.

Name: Anonymous 2009-05-20 17:17

>>39
IHBT.

Name: Anonymous 2009-05-20 20:30

>>1
tell us what it's supposed to do, you fucking idiot. it could even be right depending on what it's supposed to do.

Name: Anonymous 2009-05-20 23:11

>>37
#define v void
#define f frozen
fv

Don't change these.
Name: Email:
Entire Thread Thread List