Unable to count blank spaces

T

The Conqueror

Guest
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

I am the night...I 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.
 
Last edited:

vickybat

I am the night...I 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. :)
 
OP
T

The Conqueror

Guest
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

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

The Conqueror

Guest
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

I am the night...I 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.
 
Last edited:

Mario

Ambassador of Buzz
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

Aspiring Novelist
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

Wise Old Owl
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

Chosen of the Omnissiah
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

I am the night...I am...
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.
 
Top Bottom