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 4:06

>>90
A set is a collection of objects. If X is a set and y is an object, the statement y is an element of X is either true or false.

R+ is the set of non negative real numbers. { r in R | r >= 0 }

A function is a mapping from a domain set to a range set. For each element in the domain, there is a unique corresponding value in the range set. Rigorous definition following...

If X and Y are sets, then X is said to be a subset of Y if for every considerable object o, o must be element of Y whenever o is an element of X.
o in X implies o in Y.
\forall o \in X, o \in Y


If X and Y are sets, the cartesian product of X and Y, denoted as X*Y, is the set of all pairs (x,y) such that x is an element of X and y is an element of Y.
X*Y = { (x,y) | x \in X, y \in Y }

A relation R between X and Y is a subset of the cartesian product of X and Y. If a pair (x,y) is an element of R, x and y are said to be related, or related by R.

A function f: X -> Y is a relation of X and Y such that each element of X is related by f to a unique element of Y.

\forall x \in X, \exists ! y \in Y, (x,y) \in f

The statement (x,y) \in f is written as f(x) = y.

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