Reads input until encountering # character - C Statement

C examples for Statement:while

Introduction

Reports the number of spaces read, the number of newline characters read, and the number of all other characters read.

Demo Code

#include <stdio.h>  
int main(void)      
{  /*from w  w w .  ja v a 2s. com*/
     char ch;  
     int sp_ct = 0;  
     int nl_ct = 0;  
     int other = 0;  
       
     while ((ch = getchar()) != '#')  
     {  
         if (ch == ' ')  
             sp_ct++;  
         else if (ch == '\n')  
             nl_ct++;  
         else  
             other++;  
     }  
     printf("spaces: %d, newlines: %d, others: %d\n", sp_ct, nl_ct, other);  
       
    return 0;  
}

Result


Related Tutorials