Preventing buffer overflow with gets()
Here is what I have going:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char phrase[40];
printf("Please enter a phrase: ");
//scanf("%s", &phrase);
gets(phrase);
printf("\nThe phrase is: %s\n", phrase);
if (phrase[0] == 'h' && phrase[1] == 'e' && phrase[2] == 'l' && phrase[3] == 'l' &&
phrase[4] == 'o')
printf("The phrase is hello.\n");
else
printf("The phrase is not hello.\n");
return 0;
}
is there a way that I can add in some sort of error control with gets() in that if the user enters more characters than this array (phrase) allows that I can have them stop and enter the characters again? in other ways... is there a way to make gets() more intelligent?
|