I have no syntactical errors, but i cant figure out why this wont work.
Heres the code.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int main (void)
{
char str[100];
int i = 0, line_count = 0, len = 0, alpha = 0, lower = 0, upper = 0, digit = 0, alphanum = 0, other = 0, string_count;
int alphasum = 0, lowersum = 0, uppersum = 0, digitsum = 0, alphanumsum = 0, othersum = 0;
printf("Please enter 5 lines of characters, press enter to continue on to the next line");
while (line_count < 5)
{
printf("->");
while (str[i] = getchar() != '\n' && i < 100)
{
i++;
}
str[i] = '\0';
line_count++;
}
for (i = 0, line_count = 0, string_count = 1; str[i] != '\0' && line_count < 5; i++, line_count++)
{
if(isalpha(str[i]) == 1)
{
alpha++;
}
if(isdigit(str[i]) == 1)
{
digit++;
}
if(islower(str[i]) == 1)
{
lower++;
}
if(isalnum(str[i]) == 1)
{
alphanum++;
}
if(isupper(str[i]) == 1)
{
upper++;
}
else
{
other++;
}
if (str[i] == '\0')
{
len = i - len;
}
printf("\nTotals for string %i:\n", string_count);
printf("String Length: %i\n", len);
printf("Alphabetic Characters: %i\n", alpha);
printf("Lowercase Characters: %i\n", lower);
printf("Uppercase Characters: %i\n", upper);
printf("Numeric Characters: %i\n", digit);
printf("Alphanumeric Characters: %i\n", alphanum);
alphasum = alpha + alphasum;
digitsum = digit + digitsum;
lowersum = lower + lowersum;
alphanumsum = alphanum + alphanumsum;
uppersum = upper + uppersum;
othersum = other + othersum;
string_count++;
}
printf("\n-------------------------------------------");
printf("\nTotals for the strings are as follows:\n");
printf("Alphabetic Characters: %i\n", alphasum);
printf("Lowercase Characters: %i\n", lowersum);
printf("Uppercase Characters: %i\n", uppersum);
printf("Numeric Characters: %i\n", digitsum);
printf("Alphanumeric Characters: %i\n", alphanumsum);
return 0;
}
Heres the assignment.
1. Declare a string, e.g. char str[81];
2. Input a string from the keyboard (spaces included) using the (while statement while( while (str = getchar() != '\n' ) adjusted so that it only inputs a certain number of characters ( in the above example that would be 80, not 81).
a) Note that the number of characters needs to be a variable, not a constant
b) Be sure that there's a null in the proper location once the input is done.
3. You are going to input a string five times, so make sure you put a loop in to repeat the input and processing (listed below)
4. Make sure the string has at least one character from each category (a-f) below. Each string you input needs to have at least 15 characters.
5. Analyze each string and output its length as well and the amount of characters in the string that fall into each of these categories - be sure to print out totals for each string then at the end for all strings combined:
a) alphabetic characters
b) lower case characters
c) upper case characters
d) numeric characters
e) alphanumeric characters
f) all others .