# Unable to count blank spaces



## The Conqueror (May 26, 2013)

The problem is to print the total number of upper case characters, lower case characters and blank spaces in a string.

Here is my code :
	
	



```
#include <stdio.h>
#include <string.h>

int main()
{
    
int blank = 0;    
    int upper=0; int lower = 0;
    char ch[80];
 int i;   
    gets(ch);
    i=0;
    while(ch[i] != ' ')
    {
                if(ch[i]>= 'A' && ch[i] <= 'Z')
                upper++;
                if(ch[i]>= 'a' && ch[i] <= 'z')
                lower++;
                i++;
                }
                                  
    
    i=0;
    while(ch[i] == ' ')
                                  {
                                                 blank ++;
                                                 i++;
                                                 }
                                  
                                  
                                  
                                                 


printf(" Uppper Case : %d, Lower Case : %d, Blank Spaces : %d", upper, lower, blank);

return 0;
}
```

Well , I do get the correct number of lower case and upper case characters. But it shows number of blank characters as 0.


----------



## vickybat (May 26, 2013)

```
#include <stdio.h>
#include <string.h>

int main()
{
    
int blank = 0;    
    int upper=0; int lower = 0;
    char ch[80];
 int i;   
    gets(ch);
    
    for(i=0;i<=sizeof(ch);i++)
    {
                if(ch[i]>= 'A' && ch[i] <= 'Z'){
                upper++;
                }if(ch[i]>= 'a' && ch[i] <= 'z'){
                lower++;
                
                }else{
                blank++;
                }
               }
                                  
    
  
                                  
                                  
                                  
                                                 


printf(" Upper Case : %d, Lower Case : %d, Blank Spaces : %d", upper, lower, blank);

return 0;
}
```

Check it now buddy.


----------



## Mario (May 26, 2013)

@vickybat: Just curious, why i++ in the "lower" block?


----------



## vickybat (May 26, 2013)

^^ That was a typo mate. That was from his code that i forgot to delete while editing. Mine is incremented in the for loop itself. 
Thanks for catching it.


----------



## Mario (May 26, 2013)

That's what I thought  Just confirming since my C skills are past rusty!!


----------



## The Conqueror (May 26, 2013)

I tried to execute your code, but now number of blank spaces and number of lower case characters is a garbage value. However, it prints the number of upper case characters correctly.


----------



## vickybat (May 26, 2013)

^^ Its a garbage value all the time. 
I think you have to pass an input string each time.


----------



## The Conqueror (May 26, 2013)

I tried this string : "Think digit forums"
No. of uppercase = 1 (correct)
lowercase = 24
blank spaces = 57
So what could be the workaround? How do I implement it?


----------



## vickybat (May 26, 2013)

```
#include <stdio.h>
#include <string.h>

int main()
{
    
int blank = 0;    
    int upper=0; int lower = 0;
    char ch[80] = {'T','h','i','n','k', 'd','i','g','i','t', 'f','o','r','u','m','s' };
 int i;   
    gets(ch);
    
    for(i=0;i<=strlen(ch);i++)
    {
                if(ch[i]>= 'A' && ch[i] <= 'Z'){
                upper++;
                }if(ch[i]>= 'a' && ch[i] <= 'z'){
                lower++;
                
                }else{
                blank++;
                }
               }
                                  
    
  
                                  
                                  
                                  
                                                 


printf(" Upper Case : %d, Lower Case : %d, Blank Spaces : %d", upper, lower, blank);

return 0;

}
```

You were inputting characters wrongly. Its giving correct now. Its now giving upper- 1, lower-15 , blank -2.


----------



## Mario (May 26, 2013)

No, whats happening is whenever its upper, it is incrementing both the upper as well as the blank var. This is because there are two if blocks instead of one single connected if block.
Try 
if upper then upper++ elsif lower then lower++ else blank++ endif; <-- That should handle any string that has ONLY upper+lower+blanks.
Of course, it will not handle chars outside the alphabet and ascii 32 range, (like punctuation or digits) and will output incorrect value for blanks if the string had any of those chars!


----------



## rijinpk1 (May 26, 2013)

Vickybat's code will print all special characters throw at its way no matter  whether it is blank or not .Also try if-else if ladder instead of putting two if's

try this
#include <stdio.h>
#include <string.h>

int main()
{
int blank = 0;    
    int upper=0; int lower = 0;
    char ch[80];
 int i;   
    gets(ch);

    for(i=0;ch_!='\0';i++)
    {
                if(ch>= 'A' && ch <= 'Z'){
                upper++;
                }
				else if(ch>= 'a' && ch <= 'z'){
                lower++;

                }
				else if (ch==' ')
				{
                blank++;
                }
    }
    printf(" Uppper Case : %d, Lower Case : %d, Blank Spaces : %d", upper, lower, blank);

return 0;
}_


----------



## gagan_kumar (May 26, 2013)

rijinpk1 said:


> Vickybat's code will print all special characters throw at its way no matter  whether it is blank or not .Also try if-else if ladder instead of putting two if's
> 
> try this
> #include <stdio.h>
> ...


_

+1 to this code vickybat's code will increment on all non alphabetical characters

and what's up with u people its a 10th class program......_


----------



## ico (May 27, 2013)

vickybat said:


> ```
> #include <stdio.h>
> #include <string.h>
> 
> ...


Don't use sizeof(string) in the loop.

sizeof(array) is not equivalent to the length of the string.


----------



## vickybat (May 27, 2013)

ico said:


> Don't use sizeof(string) in the loop.
> 
> sizeof(array) is not equivalent to the length of the string.



Yeah you're right. I've kind of forgotten a lot about c syntax as its been a while now. 
I wondered why the program behaved oddly in c counting blank spaces. I ported it to other languages like java, python and it worked fine.
*strlen()* for c gives the length and i've fixed the code. Thanks for that buddy. 

Post edited.


----------



## The Conqueror (May 29, 2013)

Thank you everyone for the replies...


----------



## way2jatin (May 31, 2013)

We should better use strlen () in the loop .. It helps


----------

