#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
printf("First name: ");
// one safe way to read a string
char first[100];
scanf("%s99", first); // note - at most 99 bytes + 1 for the NULL character
printf("Last name: ");
// another safe way to read a string
char last[100]; int pos=0;
int ch;
while ((ch = fgetc(stdin)) != EOF) {
if (isspace(ch) && pos == 0) continue;
else if (isspace(ch)) break;
last[pos] = ch;
pos++;
}
last[pos] = '\0';
printf("Type some sentence...\n");
// one way to safely read a line
char * line = NULL;
size_t numChars=0, result;
result = getline(&line, &numChars, stdin);
printf("You typed: %s\n", line);
printf("Type something else...\n");
// another safe way to read a line
char line2[100];
scanf("%99[^\n]s", line2);
printf("You typed: %s\n", line2);
// note - you could also safely read a line by using fgetc
// note - need to free line2 because it was malloc'ed
free(line); line = NULL;
return 0;
}