programming with c language (3)

/*
 using while loop and the increment operator
 the program executes differently with and without
 the fflush(stdin)

*/

#include <stdio.h>
main(){
 char ch;
 int n=0;
 while((ch=getchar()) == '#'){
  /*fflush(stdin);
  */
  ++n;
 }
 printf("You entered %d characters\n",n);
}

/*
 using command for
 in nested loop
*/
#include<stdio.h>

main(){
 int i,j;
 for(i=1;i<=5;i++){
  printf("%d - ",i);
  for(j=1;j<=i;++j)
   printf("%c",'*');
  printf("\n");
 }
 /* scroll back */
 i-=2;
 for(;i>=1;i--){
  printf("%d - ",i);
  for(j=1;j<=i;++j)
   printf("%c",'*');
  printf("\n");
 }

}

/* Program to accept characters from the keyboard till the
 user hits the <return> key.
 The program should then print the following details.
 Total no. of chars entered:
 Total no. of alphabets    :
 Total no. of lower alphabets:
 Total no. of upper alphabets:
 Total no. of spaces entered:
 Total no. of digits entered:
 Total no. of others entered:

*/
#include<stdio.h>
main(){
 char ch;
 int a=0,b=0,c=0,d=0,e=0,f=0,g=0;
 printf("Enter a string of characters:");
 while((ch=getchar()) != '\n'){
  if(ch != ' ')
   a++;
  else
   e++;
  if(ch>='A' && ch <='Z'){
    d++;
    b++;
   }
  else if(ch>='a' && ch<='z'){
    c++;
    b++;
   }
  else if(ch>='0' && ch<='9')
    f++;
  else
    g++;
 }
 printf("Total no. of chars entered:%d\n",a);
 printf("Total no. of alphabets    :%d\n",b);
 printf("Total no. of lower alphabets:%d\n",c);
 printf("Total no. of upper alphabets:%d\n",d);
 printf("Total no. of spaces entered:%d\n",e);
 printf("Total no. of digits entered:%d\n",f);
 printf("Total no. of others entered:%d\n",g);
}
 
 

Click here to go back to 'C' programming
If you have comments or suggestions, email me at nicholas@tm.net.my

This page created with Netscape Navigator Gold (17/9/1999)