Search code examples
cwhile-loopgetcharputchar

Putchar, getchar, missing character in while loop


I've got problem with this peace of code, it should change lower case letters into upper case, and turn multiple spaces into one space. So what it does wrong, it somehow cuts off the first letter like when i write "abcdefg" it givess me on the output "BCDEFG".

main(){ 
int z=0,b;  

        while ( (b = getchar() ) != '\n')
        { 
        b=b-32;
            if (b>0)
            { 
                putchar(b);
            }
            else 
            {
                if (z>=2)
                { 
                    putchar(b);
                }
                else
                { 
                    z=z+1;
                    printf(" ");
                }
            }


        }
}

Solution

  • It seems to generate all the letters for me... have you tried tracing it, to find out what it is doing at each step with the characters you entered?