Saturday, December 12, 2015

Get Alphabet Input using C Program

Program #24

Description:
To detect only character input either lower/upper character and print.  Rest of the keypress are ignored.

#include<stdio.h>
#include<conio.h>
#include<dos.h>

/* user defined Constant to restrict max length */
#define MAXLENGTH 10

/* Global String Array */
char userInput[MAXLENGTH];

int main(){
 int i, charCounter=0;
 char c=' ';

 /* prototype declaration */
 void printReceivedInputs(int);
 clrscr();

 printf("\n!Important: Maximum %d valid characters only.\n", MAXLENGTH);
 for(i=1; i<46; i++){
   printf("-");
   delay(100);
 }

 printf("\nPlease Enter Alphabets value [a-zA-Z]:\n");
 for(;!kbhit();){
    c = getch();

    /* Stop getting input, If user press below keys
     Enter Key - 13
     ESC Key   - 27
     TAB Key   -  9
     Space Bar - 32
     Backspace -  8
    */

    if (charCounter == MAXLENGTH){
      printf("\n\n\nThanks for your Input!\n");
      for (i=0; i<22; i++){
 printf("_");
 delay(100);
      }
      break;
    } else if (c == 13 || c == 27 || c == 9 || c == 32 || c == 8){
      printf("\nUser Intruption !!");
      getch();
      return 0;
    }

    /* Accept only Lower Case charaters, else ignore */
    else if (c >= 97 && c <= 122){
      userInput[charCounter++] = c;
      /* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }

    /* Accept only Upper Case charaters, else ignore */
    else if (c >= 65 && c <= 90){
      userInput[charCounter++] = c;
      /* Dynamically print the user input */
      printReceivedInputs(charCounter);
    }
 }

 printf("\n\nInput #\n%s", userInput);
 getch();
 return 0;
}

void printReceivedInputs(int charLen){
  int i;
  gotoxy(15,8);
  printf("          ");
  gotoxy(15,8);
  for (i=0; i<charLen; i++){
    printf("%c",userInput[i]);
  }
}



 

No comments:

Post a Comment

Popular Posts