A
Anthony Irwin
Hi,
I am fairly new to C and all the C books I got talk about gets() but when
I compile it says I should not use gets() because it is dangerous. I
understand that it is dangerous because it doesn't check whether there is
more characters entered by the user the what can be stored but I don't
know what the safe equivalent of gets() is.
Below is an example program I have written. (GCC is my compiler on
GNU/Linux system.)
Kind Regards,
Anthony Irwin
#include <stdio.h>
char histogram[50];
int i, space, begin, length;
int main() {
printf("\nEnter some words for the histogram: ");
sgets(histogram);
printf("\n\n");
begin = 1;
length = strlen(histogram);
for (i = 0; i <= length; i++) {
if (histogram != '\0') {
if (space == 1 || begin == 1) {
histogram = toupper(histogram);
begin = 0;
}
printf("\n\t%c", histogram);
printf("\t\t%d", i);
if (histogram == ' ') {
space = 1;
printf("\t\tSpace");
}
else {
space = 0;
}
}
}
return 0;
}
I am fairly new to C and all the C books I got talk about gets() but when
I compile it says I should not use gets() because it is dangerous. I
understand that it is dangerous because it doesn't check whether there is
more characters entered by the user the what can be stored but I don't
know what the safe equivalent of gets() is.
Below is an example program I have written. (GCC is my compiler on
GNU/Linux system.)
Kind Regards,
Anthony Irwin
#include <stdio.h>
char histogram[50];
int i, space, begin, length;
int main() {
printf("\nEnter some words for the histogram: ");
sgets(histogram);
printf("\n\n");
begin = 1;
length = strlen(histogram);
for (i = 0; i <= length; i++) {
if (histogram != '\0') {
if (space == 1 || begin == 1) {
histogram = toupper(histogram);
begin = 0;
}
printf("\n\t%c", histogram);
printf("\t\t%d", i);
if (histogram == ' ') {
space = 1;
printf("\t\tSpace");
}
else {
space = 0;
}
}
}
return 0;
}