Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
int main()
{
   int  count=0;
    char arr[7];
    printf("enter a string: ");
    scanf("%s",arr);
    for(int i=0;i<7;i++){
    switch(arr[i]){
    case 'A':
    count++;
    break;
    case 'E':
    count++;
    break;
    case 'I':
    count++;
    break;
    case 'O':
    count++;
    break;
    case 'U':
    count++;
    break;
    case 'a':
    count++;
    break;
    case 'e':
    count++;
    break;
    case 'i':
    count++;
    break;
    case 'o':
    count++;
    break;
    case 'u':
    count++;
    break;
    }
    }
    printf("\nthe number of vowels is: %d",count);

    return 0;
}


What I have tried:

I solved the question but i need to edit it without the loop
Posted
Updated 9-Mar-21 11:21am

Quote:
How to write the following code to get the same output without using any kind of loop?

Technically, it is impossible.
Even CPallini's clever Solution using recursion is a loop in disguise.
With this recursion, code do not show loop structure, but compiler transforms the code to a loop.
C++
// loopish version of CPallini solution
int vcount(const char * s)
{
  int ret=0;
  while (! *s) {
    ret += iv[(unsigned)*s]
    s++
  }
  return ret;
}

This particular recursion is named: Tail call - Wikipedia[^]
-----
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
int main()
{
	int  count=0;
	char arr[7];
	printf("enter a string: ");
	scanf("%s",arr);
	for(int i=0;i<7;i++){
		switch(arr[i]){
		case 'A':
			count++;
			break;
		case 'E':
			count++;
			break;
		case 'I':
			count++;
			break;
		case 'O':
			count++;
			break;
		case 'U':
			count++;
			break;
		case 'a':
			count++;
			break;
		case 'e':
			count++;
			break;
		case 'i':
			count++;
			break;
		case 'o':
			count++;
			break;
		case 'u':
			count++;
			break;
		}
	}
	printf("\nthe number of vowels is: %d",count);

	return 0;
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
 
Share this answer
 
v2
Comments
jeron1 9-Mar-21 20:15pm    
Couldn't someone have 7 switch statements inline, one for every arr[i]? Yeah, it's awful, but possible I suppose.
Patrice T 9-Mar-21 20:20pm    
Sure they can.
CPallini 10-Mar-21 2:05am    
Indeed it is a loop in disguise.
Have my 5.
Patrice T 10-Mar-21 6:28am    
Thank you
With C, you can unleash your creativity
C
#include <stdio.h>

const int iv[] =
{
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
  0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};


int vcount(const char * s)
{
  if (! *s)
    return 0;
  return iv[(unsigned)*s] + vcount(s+1);
}

int main()
{
  char str[0x100];
  printf("please enter a string\n");
  fgets(str, 100, stdin);
  printf("%d vowels found.\n", vcount(str));
  return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 9-Mar-21 16:05pm    
+5. Brilliant.
CPallini 10-Mar-21 2:02am    
Thank you.
Patrice T 9-Mar-21 17:34pm    
+5 too
CPallini 10-Mar-21 2:02am    
Thank you.
If you need to check every character in the string then you need the loop. But your switch block Could be made much simpler:
C++
switch(tolower(arr[i]))
{
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        count++;
        break;
    default: // all other charactgers
        break;
}
 
Share this answer
 
Comments
CPallini 10-Mar-21 2:03am    
My 5.
Richard MacCutchan 10-Mar-21 3:22am    
Thanks, but your solution is far more elegant.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900