Heres the complete program. It does work how I need it to with gets, just wanted to address the buffer overflow issue (Have kept array very small to make it easier to test)
Code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main(void)
{
const int MAXSIZE = 5;
char string[MAXSIZE], b[5], c;
int len, i = 0, words = 1, function = 0, characters = 0, vow = 0, cons = 0, spaces = 0, dig = 0, other = 0;
//
//Variables declared above and menu options are below.
//
printf("Please choose option 1 initially to get started\n\n\n");
while (function != 6)
{
function = 0;
printf("\n1. Enter a words or a sentence\n");
printf("2. Find word count\n");
printf("3. Find character count\n");
printf("4. Find the number of vowels and consonants\n");
printf("5. Find if the string is a palindrome\n");
printf("6. Quit\n\n");
scanf("%d", &function);
getchar();
switch (function)
{
//
//The options chosen above now select from the list of cases
//
case 1:
printf("\nEnter a string of words:\n\n");
fgets (string, MAXSIZE, stdin);
len = strlen(string);
if (string[len-1] == '\n')
string[--len] =0;
if (string==MAXSIZE)
printf("Too many characters please try again");
break;
case 2: // This case counts the number of words in the string by counting spaces //
words = 1;
for (i = 0; i < strlen(string); i++)
if (string[i] == ' ')
words++;
printf("\nThe word count is %d\n\n", words);
break;
case 3:// This case counts the number of alphabet characters only //
characters = 0;
for (i = 0; i < strlen(string); i++)
if (isalpha(string[i]))
characters++;
printf("\nThe character count is %d\n\n", characters);
break;
case 4:// This case converts all characters to lower case the analyses it //
vow = 0;
cons = 0;
dig = 0;
spaces = 0;
other = 0;
i = 0;
while ((c = tolower(string[i++])) != '\0')
{
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
++vow;
else if (c >= 'a' && c <= 'z')
++cons;
else if (c >= '0' && c <= '9')
++dig;
else if (c == ' ')
{
++spaces;
while (string[i] == ' ' || string[i] == '\t')
{
i++;
spaces++;
}
}
else
other++;
}
printf("\nThe total number of vowels is %d\n", vow);
printf("The total number of consonants is %d\n", cons);
printf("The total number of numeric characters is %d\n", dig);
printf("The total number of special characters is %d\n", other);
printf("The total number of line spaces is %d\n\n", spaces);
break;
case 5: // This case tests for reversable text //
strcpy(b, string);
strrev(b);
if (strcmp(string, b) == 0)
printf("\nThe string is a palindrome\n\n");
else
printf("\nThe string is not a palindrome\n\n");
break;
case 6:
break;
default:
printf("\nError please enter only between 1 and 5\n");
break;
}
}
return 0;
}