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-04 0:02

>>43
It's an integer type just like short, long, int and whatnot.  It is ``meant for'' holding characters, but it can hold any integer value within its range.
I never said it wasn't an integer type, however it's not a "signed integer type" which the other types you listed are, signed char is a signed integer type.

If you write short int, int or long int you know that they are signed, you don't know that about char, whether char is signed or unsigned is up to the implementation so if you want to be portable you can only work with the 0-127 range i.e. 7 bits unless you specify with either the signed or unsigned type specifiers.

char is not necessarily 8 bits.
Right, it has to be able to hold at least 8 bits.

int is not necessarily larger than char.
Right, but why are you telling me this?

sizeof(char) is guaranteed to be 1, however, meaning the sizes of all other types must be multiples of the size of char.
Right, but why are you telling me this?

>>45
No, seriously, if sizeof(char) is guaranteed to be 1, why wouldn't char be 8 bits?
It's at least 8 bits, of which you can only 7 bits if you want to be portable when you're using the unspecified char type.

On systems that support the optional types in stdint.h like int8_t and uint8_t CHAR_BIT is exactly 8.

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