0

** I understood how the function getline is working, it simply assigning value in each s[] array address which gets stored into the char line[] array because function argument has local scope there is no conflict due to the use of different array names unless it shares the same data type, But my concern is that why checker function has no effect on an actual string. correct me if the above understanding is wrong and why the checker function not working.** the expected result was, getting string without trailing blanks and tabs like (hello World) but instead of that actual input that I typed in is printed which is ('\s'hello World'\t').

#define MAXLINE 50

int getline(char line[], int max);
int checker(char line[]);

int main(){
    char line[MAXLINE];

    while( getline(line, MAXLINE) > 0 )
        if( checker(line) > 0 )
            printf("%s",line);
    return 0;
}


int getline(char s[],int lim){
    int c,i,j;
    j=0;

    for(i=0; (c=getchar()) != EOF && c != '\n';i++){
        if(i < lim-1){
            s[j]=c;
            ++j;
        }
    }
    if(c == '\n'){
        s[j] = c;
        ++j;
        ++i;
    }
    s[j] = '\0';
    return i;
}


int checker(char s[]){
    int i;
    i=0;

    while(s[i] != '\n' )
        ++i;
    --i;
    while(i >= 0 && (s[i] == ' ' || s[i] == '\t') )
        i++;
    if( i >= 0){
        ++i;
        s[i] = '\n';
        ++i;
        s[i] = '\0';
    }
    return i;
}
  • "*why checker function has no effect on an actual string*". Run the program in a debugger and step thru it line by line to see exactly what it is doing. The `checker` function definetely *can* have an effect on the string. But it is conditional (inside the `if( i >= 0)` block). So it depends on what the input is (which you have not provided). – kaylum Jul 04 '21 at 04:48
  • "*why the checker function not working*". "Not working" is never a good problem description. Please give the exact input, expected result and actual result. – kaylum Jul 04 '21 at 04:51
  • the expected result was, getting string without trailing blanks and tabs but instead of that actual input that I typed in is printed. – kshitiz ghimire Jul 04 '21 at 05:05
  • "... due to the use of different array names ..." The names are irrelevant. You could have used `line` instead of `s` in `getline` – Support Ukraine Jul 04 '21 at 05:07
  • 1
    Please [Edit](https://stackoverflow.com/posts/68241745/edit) the post with that info that is needed to understand the question. And please give the **exact** input, expected result and actual result. – kaylum Jul 04 '21 at 05:07
  • Input? Expected output? Actual output? – Support Ukraine Jul 04 '21 at 05:07
  • 1
    And have you done the suggested debugging? Before asking for an explanation it is better to use the tools already available to you to try and understand what the code is doing. Learning to debug is an essential skill for any developer. [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – kaylum Jul 04 '21 at 05:09
  • "*without trailing blanks and tabs but instead of that actual input that I typed in is printed*. How do you know the output is wrong since the output with and without "trailing blanks and tabs" would look the same? So repeat: Please provide exactly what you are typing in, what the exact output is you see and what you expected instead. – kaylum Jul 04 '21 at 05:16
  • @kaylum i tried but couldn't figure out the real problem . It still printing string with trailing blanks and tabs, at printf("%s",line); – kshitiz ghimire Jul 04 '21 at 05:54

2 Answers2

0

The bug seems to be here:

while(i >= 0 && (s[i] == ' ' || s[i] == '\t') )
    i++;
    ^^^^
    This shall probably be i--;

That said... Your function isn't secure. It lacks some checks to prevent access outside the char array.

For instance:

  • What happens if the input string has no '\n' ?

  • What happens if the input string is a space followed by '\n' ?

Also the getline function has a problem. If the input is longer than lim, the code will do s[lim] = '\0'; which is out of bounds.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

If you are trying to trim trailing blanks and tabs from your string, try changing the contents of the second while loop in checker() to contain i-- rather than i++.

Since checker() is intended to change the string, perhaps a different name would be better. The word check does not usually imply modification. A well chosen name is a great help to the next person who encounters your code.

DavidHoadley
  • 301
  • 1
  • 7