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 .

I've gotten almost everything to work except the output, and i believe it only needs minor tweaking. I have no known syntactical errors but am always welcome to suggestions to make the code better.

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; i < 100 ; i++)
{

if (str[i] == '\0' && string_count < 6) 
{
len = len + i;
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);
printf("Other:%i\n",other);

string_count++;

alphasum = alpha + alphasum;
digitsum = digit + digitsum;
lowersum = lower + lowersum;
alphanumsum = alphanum + alphanumsum;
uppersum = upper + uppersum;
othersum = other + othersum;

}

else if(isalpha(str[i]) == 1)
{	
alpha++;
}
else if(isdigit(str[i]) == 1)
{
digit++;
}
else if(islower(str[i]) == 1)
{
lower++;
}
else if(isalnum(str[i]) == 1)
{
alphanum++;
}
else if(isupper(str[i]) == 1)
{
upper++;
}

else
{
other++;
}

alphasum = alpha + alphasum;
digitsum = digit + digitsum;
lowersum = lower + lowersum;
alphanumsum = alphanum + alphanumsum;
uppersum = upper + uppersum;
othersum = other + othersum;

}
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);
printf("Other:%i\n", othersum);

return 0;
}

Recommended Answers

All 3 Replies

Major thing to correct is formatting. You have none. Because of that your code is too hard to follow.

Line 88

#
printf("\n---------------------------…

Is incorrect.

Also, your prompting for 5 strings but I only see one place that you store the values

char str[100];

I re-wrote some of your code implementing a multi-dimensional array...Please note the comments about line's greater than 100 characters.

#include <stdio.h>

#define LINE_SIZE 100

int main (void)
{
	int line_count = 0, i = 0;
	char 	str[5][100];

	printf("Please enter 5 lines of characters, press enter to continue on to the next line");

	while (line_count < 5)
	{
		printf("->");

		while ((str[line_count][i] = getchar()) != '\n' && i < LINE_SIZE)
		{ 
			i++; 
		}

		/*you will have problems here if your line(string) is greater than 100*/

		str[line_count][i] = '\0';

		line_count++;
		i = 0;
	}

	line_count = 0;
	i = 0;

	while (line_count < 5)
	{
		fprintf(stdout, "string->%s\n", &str[line_count++][0]);
	}
	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.