DIGIT-ARC read-only archive · snapshot: July 23, 2026

Unable to count blank spaces

16 posts · originally from https://geek.digit.in/community/threads/unable-to-count-blank-spaces.174186/

T
The Conqueror Original post #1
May 26, 2013 at 12:00 AM
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 :
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 at 12:42 AM
PHP:
#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.
M
Mario #3
May 26, 2013 at 12:50 AM
@vickybat: Just curious, why i++ in the "lower" block?
vickybat
May 26, 2013 at 12:55 AM
^^ 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. :)
M
Mario #5
May 26, 2013 at 1:01 AM
That's what I thought :) Just confirming since my C skills are past rusty!!
T
The Conqueror #6
May 26, 2013 at 1:04 AM
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 at 1:07 AM
^^ Its a garbage value all the time.
I think you have to pass an input string each time.
T
The Conqueror #8
May 26, 2013 at 1:14 AM
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 at 1:28 AM
PHP:
#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.
M
Mario #10
May 26, 2013 at 8:40 AM
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
rijinpk1 #11
May 26, 2013 at 10:30 AM
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 at 11:11 AM
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;
}


+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......
skeletor
skeletor #13
May 27, 2013 at 9:14 AM
PHP:
#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<=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;

}

You were inputting characters wrongly. Its giving correct now. Its now giving upper- 1, lower-15 , but blank seems to be garbage value.
Use a separate loop to count blank.
Don't use sizeof(string) in the loop.

sizeof(array) is not equivalent to the length of the string.
vickybat
vickybat #14
May 27, 2013 at 12:58 PM
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.
T
The Conqueror #15
May 29, 2013 at 9:10 PM
Thank you everyone for the replies... :)
W
way2jatin #16
May 31, 2013 at 3:34 PM
We should better use strlen () in the loop .. It helps