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

Fucking C

Name: Anonymous 2012-11-03 14:43

Why it doesn't show me strings like "  a"?
Why is the while loop in the trim function bugged (if I input 2 chars the next input length can't be less than 2)?

#include <stdio.h>

#define MAX 1001

int getLine(char s[], int length){ // returns 1 or 0

    int i, qttWord = 0; //qttWord = counter of letters for s[]
    int c; // c = getchar()

    /*Reads the input and puts it into s[], then, verifies if the input is just \n,
    * if so, returns 0(i), if not, puts '\0' at the end of the string.
    */
    for (i = 0; i < length-1 && (c = getchar()) != EOF && c != '\n'; ++i){
            s[i] = c;
            ++qttWord;

    }
    if (i == 0){
        return 0;
    } else if (c == '\n'){
        ++i;
        s[i] = '\0';

    }
    /*Verifies if the string is just ' ' or '\t'
    * if so, returns 0
    */
    char flag = '\0';
    for (i = 0; i < qttWord && flag != '1'; ++i){
        if (s[i] == ' ' || s[i] == '\t'){
            flag = '0';
        } else{
            flag = '1';
        }
    }
    if (flag == '0')
        return 0;

    return qttWord;
}
void trim(char s[], int length){

    char s2[MAX];
    int i, qttWord = 0;

    /*while (s[length] != '\0'){
        ++length;
    }
    printf("length:%d\n", length);*/
    for (i = 0; i < length; ++i){
        if (i < length-1){
            if (s[i] == ' ' && s[i+1] != ' '){
                s2[i] = s[i];
                ++qttWord;
                printf("1:%d\n", s2[i]);// if true prints "1" and the character(' ')
            }
        }
        if (s[i] != ' '){
            s2[i] = s[i];
            ++qttWord;
            printf("0:%d\n", s2[i]);//if true prints "0" and the character
        }
    }
    s[0] = '\0';
    s2[qttWord] = '\0';

    for (i = 0; i < qttWord; ++i){
        s[i] = s2[i];
    }
    s2[0] = '\0';
}

int main(){
    char line[MAX];
    int lgh = 0;

    while ((lgh = getLine(line, MAX)) != 0){
        trim(line, lgh);
        printf("%s\n", line);
        line[0] = '\0';
        lgh = 0;
    }
    return 0;
}

Name: Anonymous 2012-11-05 6:15

>>103
Are you arguing against those rights, cretin?  We already know that you have a problem with the mere existence of racist/sexist/homophobic humour (even if done sarcastically), which makes you Nazi freedom-of-speech-hating cretin, let's see which of those rights you'll argue against next.  Perhaps you'll tell me that your lovely ideal Electronic Police State should be allowed to lawfully access the contents of any computer without a warrant at any time, and that cryptography is evil and should be banned because some terrorist/paedophile/serial-murderer/homophobic-joke-utterer was caught using it at some point.  Or maybe animal agriculture should be banned entirely because humans are too righteous and moral to do such a lowly animalistic thing as eating meat.  Who knows what retarded thing you'll argue against next.

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