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 .

Recommended Answers

All 9 Replies

Hello
your point of error is

for (i = 0, line_count = 0, string_count = 1; str[i] != '\0' && line_count < 5; i++, line_count++)

check the conditional and increment/decrement part in this for loop
this loop only execute for five character not for five line because you increase the value of line_count at every character check.

second thing is that you print your detail for every character not for line

Try solve all this problem then ask if get any more problem

Ive attempted to fix and am still running into problems.

#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(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++;
		}
		
		
		
		string_count++;
	
		if (str[i] == '\0') 
		{
			len = i - 1 - 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;
		digitsum += digit;
		lowersum += lower;
		alphanumsum += alphanum;
		uppersum += upper;
		othersum += other;
		
		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;
}

For your while loop, you are accepting 5 strings but you never SAVE any but OVERWRITE it again and again. As a result, you will have only the last string left to work with. What you need to do is to move your for-loop inside line_count while loop but below the inner while loop. Also, you may use while loop instead of for loop. The reason is that you are supposed to count whatever entered which may or may not be as long as 100 chars. Just iterate through the string from 0 upto whatever character is not '\0'. After you move the counting loop inside while loop, line_count will not be needed in the counting loop.

For your else statement inside for-loop, are you sure that 'all other' means isUpper()!=1 because your 'else' will nest with its immediate 'if' statement? I believe you will need a flag in order to determine whether value of any of alpha, digit, lower, alphanum, or upper is increment in a loop. If none of those variables' value is increased, then it is 'all other'.

Oh and what 'string_count' is for???

How does it rewrite? all it does it move characters from the keyboard buffer to a character array, in sequential order. i fixed the if statements. But am still having problems. heres the updated 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;
}

Err... Looks like you have no idea about how to solve the problem... You must not use full if-else statement to determine the categories. Unless I misunderstand the meaning of each category, they are not all mutual exclusive except the first 6 and the last one. For example, 'a' is an alphabet and is a lowercase.

Here is an example of the code structure modifying from your original code...

while (line_count < 5) {
  printf("->");
  // One issue that may occur using this while loop is that when a user
  // enters backspace or any non-displayable char. I'm not going to fix it
  // for you, so you could practice how to do it.
  string_count = 0;
  while ((str[i] = getchar())!='\n' && i<99) { // save the last one for '\0'
    i++; 
  }
  str[i] = '\0';  // add end of string

  // This loop is moved in here
  // Also, I just use the length of the string you counted already
  // so I don't need to increment the 'i' after adding the end of string char
  // If you are going to do this loop outside this while loop, you need
  // to increment 'i' and you need a place to store all string lines.
  for (int pos=0; pos<i; pos++) {
    // check each str[pos] and increment category counters
  }
  // display the result of this string here

  line_count++;  // counter for number of string
}

// display result summary of all strings here

The way the code structure above will output result for each enter string. Then you need to output the whole summary at the end again.

Thanks again for your help. I really appreciate it. As you can probably tell im pretty new to programming, and have my downfalls. I kind of figured the if- else statements werent working. But i dont really know another way to do that kind of logic unfortunately. I do have a few questions:

1) string_count = 0;? is kind of pointless now yeah? since you seem to be using line_count as your counter instead.
2) Is it ok that im entering all the strings into one char array and not a multi-dimensional?

3) Do i have to clear the counters for the alpha, digit, alphanumeric, ect each time the while loop goes through because i just want to display the total for THAT string and not the total accumulated throughout the process.

4) Should i continue to use my sum counter so i am able to display the sums at the end?

i think that should do it. Once again thanks for you help. Really means a lot.

1)Oops, I left it in there, sorry. You are right that you don't need it.
2)It is OK as long as you are going to go through the string to find the result for each string right away.
3)Yes, you have to for each string; however, remember to keep the sum values for them.
4)Yes, please do.

About your if-else for checking, it could be as followed...

bool isOther = true;
if(isalpha(str[i]) == 1) {	
  alpha++;
  isOther = false;
}
if(isdigit(str[i]) == 1) {
  digit++;
  isOther = false;
}
if(islower(str[i]) == 1) {
  lower++;
  isOther = false;
}
if(isalnum(str[i]) == 1) {
  alphanum++;
  isOther = false;
}
if(isupper(str[i]) == 1) {
  upper++;
  isOther = false;
}
if (isOther) {  // not fall into any of first 6 categories
  other++;
}

Alright i got everything else to work but the counters. They just seem to add every time the loop iterates. Same goes for the length counter, which i made a function. And i've tried setting the values to zero at the end of the loop but it doesn't really seem to work. So once again ive confused myself.

1) Why do all the values not reset to zero in the middle of the loop where i have them set to zero?
2) How can i utilize strlen() to find the length of EACH string and not the combined value?

I believe these are the last of my questions... :-/ sorry again for being such a newbie.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>

int main (void)
{
	char str[100];
	int i = 0, line_count = 0, alpha = 0, lower = 0, upper = 0, digit = 0, alphanum = 0, other = 0;	
	int alphasum = 0, lowersum = 0, uppersum = 0, digitsum = 0, alphanumsum = 0, othersum = 0, pos = 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<99) 
		{
			i++; 
		}
		
		str[i] = '\0';
		
		alpha = 0, lower = 0, upper = 0, digit = 0, alphanum = 0, other = 0;
		
		for (pos = 0; pos < i; pos++) 
		{
			bool isother = true;
			
			if(isalpha(str[pos]))
			{	
				alpha++;
				isother = false;
			}
			if(isdigit(str[pos]))
			{
				digit++;
				isother = false;
			}
			if(islower(str[pos]))
			{
				lower++;
				isother = false;
			}
			if(isalnum(str[pos]))
			{
				alphanum++;
				isother = false;
			}
			if(isupper(str[pos]))
			{
				upper++;
				isother = false;
			}
			if (isother) 
			{
				other++;
			}
		}
		
		printf("\nTotals for string %i:\n", line_count);
		printf("String Length: %i\n", strlen(str));
		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);
		
		line_count++;
		
		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;
}

You need to reset the length counter ( i=0; ) below line 16. Sorry about forgetting this (again :().

1)Because your syntax is not correct.

// should use semi-colon because you are not declaring it but now assigning value
alpha = 0; lower = 0; upper = 0; digit = 0; alphanum = 0; other = 0;

2)You could use strlen() in place of 'i' in (pos=0; pos<i; pos++). As I said earlier, I already knew the string length by the time I got to there, so I didn't use any string length function.

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.