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.
This is because
scanf()doesn’t include the linefeed, it considers it whitespace and uses whitespace to separate the values it converts. Just usestrlen()‘s return value.