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

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

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

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