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

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: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.

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