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

Pages: 1-

Trim problem

Name: Anonymous 2012-10-31 13:43

Ok, more readable...

#include <stdio.h>

#define MAX 1000

int main(){
    char line[MAX];
    int lgh;// 1 or 0
    line[0] = '\0';
    while ((lgh = getLine(line, MAX)) != 0){
        printf("%s\n", line);
        line[0] = '\0';
    }
    return 0;
}

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){
        if (c == '\n')
            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;
    /*
    *The trim function
    */
    char s2[length];
    s2[0] = '\0'; // temp char array
    int qttWord2 = 0; //qttWord2 = counter of letters for s2[]
    for (i = 0; i <= qttWord; ++i){
        if (i < qttWord-1){
            if (s[i] == ' ' && s[i+1] != ' '){
                s2[i] = s[i];
                ++qttWord2;
                printf("1%c\n", s2[i]);// if true prints "1" and the character(' ')
            }
        }
        if (s[i] != ' '){
            s2[i] = s[i];
            ++qttWord2;
            printf("0%c\n", s2[i]);//if true prints "0" and the character
        }
    }
    s[0] = '\0';
    for (i = 0; i < qttWord2; ++i){
        s[i] = s2[i];
        printf("C:%c\n", s[i]); //shows what's is happening
    }
   
    return 1;
}

Why the trim isn't working?

Name: Anonymous 2012-10-31 13:44

Use [code] tags.

Name: Anonymous 2012-10-31 13:44

>>2
I am using?

Name: Anonymous 2012-10-31 13:46

*//what is happening

Name: Anonymous 2012-10-31 13:52

>>3
Check my sage field.

Name: Anonymous 2012-10-31 13:56

>>5
Who cares?

Name: Anonymous 2012-10-31 14:00

You have a talent OP.

Name: >>7 2012-10-31 14:02

Unfortunately for you, it is entirely unrelated to programming.

Name: Anonymous 2012-10-31 14:06

>>7
>>8
Cool.
Now explain why the trim isn't working, expert programmer.

Name: Anonymous 2012-10-31 14:09

>>9
Read SICP.

Name: Anonymous 2012-10-31 14:23

This can't be accidental.

Name: Anonymous 2012-10-31 14:27

void trim(const char * in, size_t in_len, char * out){
    size_t right_bound = in_len;
    while(right_bound > 0 && in[right_bound] == ' ')
        --right_bound;
    size_t left_bound = 0;
    while(left_bound < right_bound && in[left_bound] == ' ')
        ++left_bound;
    for(int i = 0; i < right_bound - left_bound; ++i)
        out[i] = in[i + left_bound];
}

Name: Anonymous 2012-10-31 14:30

>>12
No pointers please.

Not yet.

Name: Anonymous 2012-10-31 14:36

Can you fix my Lisp assignment?
No parentheses please.

Not yet.

Name: Anonymous 2012-10-31 14:39

/* Trim shit, assuming this exists:
    char in[MAX];
    char out[MAX];
*/
    size_t right_bound = strlen(in);
    while(right_bound > 0 && in[right_bound] == ' ')
        --right_bound;
    size_t left_bound = 0;
    while(left_bound < right_bound && in[left_bound] == ' ')
        ++left_bound;
    for(int i = 0; i < right_bound - left_bound; ++i)
        out[i] = in[i + left_bound];
/*
    Continue doing something retarded here
*/

Name: Anonymous 2012-10-31 14:47

actually it had some bugs
    size_t right_bound = strlen(in);
    while(right_bound > 0 && in[right_bound] == ' ')
        --right_bound;
    size_t left_bound = 0;
    while(left_bound < right_bound && in[left_bound] == ' ')
        ++left_bound;
    for(int i = 0; i < right_bound - left_bound + 1; ++i)
        out[i] = in[i + left_bound];
    out[right_bound - left_bound + 1] = 0;

Name: Anonymous 2012-10-31 16:01

>>14
Fuck I'm not at pointers yet!

Name: Anonymous 2012-10-31 17:15

>>17
Then learn them you lazy fuck

Name: Anonymous 2012-10-31 19:23

This is how you segfault the gibson

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