Checking for an "end of line" in a C-string

Posted by Numerator on Stack Overflow See other posts from Stack Overflow or by Numerator
Published on 2012-06-05T16:32:20Z Indexed on 2012/06/05 16:40 UTC
Read the original article Hit count: 139

Filed under:

I would really love your help with the following problem:

I want to get as an input from the user a maximum length of 30 chars string and check whether it contains an end of line.

This is what I tried to write so far:

int main(void) {
int i;
char* command = (char*)calloc(31, sizeof(char));
while (0 < 1) {
    scanf("%s", command);
    for (i = 0; i <= strlen(command); ++i) {
        if (command[i] == '\n')
            printf("here");
    }
    if (strcmp(command, "quit") == 0)
        break;
}

The idea is to check whether the command given by the user as input is "legal" - that is of length < 31. when i run this code, it never prints "here" regardless of the length of input.

© Stack Overflow or respective owner

Related posts about c