Yesterday, I posted a similar problem (involving numbers) which was solved by Clinton Portis (Thanks by the way) which involved several if statements. This time I got one with a string :rolleyes:

If there's some kind of space saving technique please let me know :mrgreen:

#include <stdio.h>

#define SIZE 5

int main(void)
{
	char ch, array[SIZE] = {0};
	int a = 0, e = 1, i = 2, o = 3, u = 4;
	int cnt = 0;

	while ((ch = getchar()) != NULL && ch != '\n')
	{
		if (ch == 'a')
			++array[0];
		if (ch == 'e')
			++array[1];
		if (ch == 'i')
			++array[2];
		if (ch == 'o')
			++array[3];
		if (ch == 'u')
			++array[4];
		cnt++;
	}

	printf("You entered %d letters\n", cnt);
	printf("Number of 'a': %d & Percent of Total: %.2f\n", array[0], (float) array[0]/cnt*100);
	printf("Number of 'e': %d & Percent of Total: %.2f\n", array[1], (float) array[1]/cnt*100);
	printf("Number of 'i': %d & Percent of Total: %.2f\n", array[2], (float) array[2]/cnt*100);
	printf("Number of 'o': %d & Percent of Total: %.2f\n", array[3], (float) array[3]/cnt*100);
	printf("Number of 'u': %d & Percent of Total: %.2f\n", array[4], (float) array[4]/cnt*100);

	return 0;
}

Recommended Answers

All 4 Replies

If you posted the entire function then what you did is probably as simple as any. you could use a switch statement, but it doen't shorten or reduce your program and might actually increase the bulk of the program.

switch(ch)
{
  case 'a': ++array[0]; break;
  ...
}

If you need to evaluate all (or most) of the 255 possible characters, then you could use the character as the index into the array.

int array[255] = {0};
...
int ch;
while( (ch = getch() ...)
{
   ++array[ch];
}

>while ((ch = getchar()) != NULL && ch != '\n')
NULL is not the same as EOF, and it never will be. Also, ch should be declared as int because char might be unsigned and EOF is a negative value.

>If there's some kind of space saving technique please let me know
You could use a table lookup, but the result would either use a loop, or wrap some form of translation from characters to indices. The wrapper would be longer in this case, and the loop isn't much shorter. You don't really see the benefits until the table holds more values:

#include <stdio.h>

#define SIZE 5

int main(void)
{
  int ch;
  char array[SIZE] = {0};
  char match[] = "aeiou";
  int cnt = 0;

  while ((ch = getchar()) != EOF && ch != '\n')
  {
    int i;

    for ( i = 0; match[i] != '\0'; i++ ) {
      if ( ch == match[i] ) {
        ++array[i];
        break;
      }
    }

    cnt++;
  }

  printf("You entered %d letters\n", cnt);
  printf("Number of 'a': %d & Percent of Total: %.2f\n", array[0], (float) array[0]/cnt*100);
  printf("Number of 'e': %d & Percent of Total: %.2f\n", array[1], (float) array[1]/cnt*100);
  printf("Number of 'i': %d & Percent of Total: %.2f\n", array[2], (float) array[2]/cnt*100);
  printf("Number of 'o': %d & Percent of Total: %.2f\n", array[3], (float) array[3]/cnt*100);
  printf("Number of 'u': %d & Percent of Total: %.2f\n", array[4], (float) array[4]/cnt*100);

  return 0;
}

This is just my opinion, but if what you're writing makes sense, don't worry about making control logic as compact as possible until you start having large numbers of cases.

I'd personally go with a switch statement like Ancient Dragon posted, but it's a matter of taste.

The physical representation would be pushbuttons on a blender (repeated ifs) vs. the channel dial on an old TV (switch statement).

>while ((ch = getchar()) != NULL && ch != '\n')
NULL is not the same as EOF, and it never will be. Also, ch should be declared as int because char might be unsigned and EOF is a negative value.

>If there's some kind of space saving technique please let me know
You could use a table lookup, but the result would either use a loop, or wrap some form of translation from characters to indices. The wrapper would be longer in this case, and the loop isn't much shorter. You don't really see the benefits until the table holds more values:

#include <stdio.h>

#define SIZE 5

int main(void)
{
  int ch;
  char array[SIZE] = {0};
  char match[] = "aeiou";
  int cnt = 0;

  while ((ch = getchar()) != EOF && ch != '\n')
  {
    int i;

    for ( i = 0; match[i] != '\0'; i++ ) {
      if ( ch == match[i] ) {
        ++array[i];
        break;
      }
    }

    cnt++;
  }

  printf("You entered %d letters\n", cnt);
  printf("Number of 'a': %d & Percent of Total: %.2f\n", array[0], (float) array[0]/cnt*100);
  printf("Number of 'e': %d & Percent of Total: %.2f\n", array[1], (float) array[1]/cnt*100);
  printf("Number of 'i': %d & Percent of Total: %.2f\n", array[2], (float) array[2]/cnt*100);
  printf("Number of 'o': %d & Percent of Total: %.2f\n", array[3], (float) array[3]/cnt*100);
  printf("Number of 'u': %d & Percent of Total: %.2f\n", array[4], (float) array[4]/cnt*100);

  return 0;
}

wow thx.

I didn't even think of putting the letters into another array and then searching :eek:

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.