All validation in c programs

1.Check the user input Length

#include<stdio.h>
#include<conio.h>
void main()
{
    int a;
   
    do{
    printf("Enter the length 1 to 100\n");
    scanf("%d",&a);
    if(a>=1 && a<=100)
    {
printf("data in the range:%d",a);
break;
    }
    else
    {
        printf("\n data out of range\n");
    }
     
  
    }while(1);  
    getch();
}

output:-

Enter the length 1 to 100

6667

 data out of range

Enter the length 1 to 100

45

data in the range:45


2.Check menu choice

#include <stdio.h>
#include<stdlib.h>
int main(void) {
 int menu;
 printf("\n1:for hello \n2: for exit");
 printf("\nEnter Menu Option: ");
 scanf("%d", &menu);
switch (menu)
{
case 1:
    printf("\n hello");
    break;
case 2:
exit(0);
default:
printf("\ninvalid choice");
    break;
}
}
output:-

1:for hello

2: for exit

Enter Menu Option: 3

invalid choice


3.Validate String and Number

example 1:-
 #include <string.h>
  #include <stdio.h>

  void checkTriangle(char *side1)
  {
    int i;
    int found_letter = 0;
    int len = strlen(side1);

    for( i = 0; i < len; i++)
    {
        if(side1[i] < '0' || side1[i] > '9')
        {
            found_letter = 1; // this variable works as a boolean
            break;
        }
    }
    if(found_letter) // value 0 means false, any other value means true
        printf("You entered a string");
    else
        printf("You entered only numbers");
  }
  void main()
  {
      checkTriangle("113");
  }

output:-
You entered only numbers
Example 2:

 #include <string.h>
  #include <stdio.h>

  void checkTriangle(char *side1)
  {
    int i;
    int found_letter = 0;
    int len = strlen(side1);

    for( i = 0; i < len; i++)
    {
        if(side1[i] < '0' || side1[i] > '9')
        {
            found_letter = 1; // this variable works as a boolean
            break;
        }
    }
    if(found_letter) // value 0 means false, any other value means true
        printf("You entered a string");
    else
        printf("You entered only numbers");
  }
  void main()
  {
      checkTriangle("hello");
  }
output:-
You entered a string

4.compare string

#include<stdio.h>
#include<string.h>
void main()
{
    char a[10];
    printf("enter the string:");
    scanf("%s",&a);
    if(stricmp(a,"hello")==0)
    {
        printf("same");
    }
    else{
        printf("different");
    }

}
output:-
enter the string:hello
same
output:-
enter the string:hey  
different

4.Validate Date in c program

/*C program to validate date (Check date is valid or not).*/
#include <stdio.h>
int main()
{
int dd,mm,yy;
printf("Enter date (DD/MM/YYYY format): ");
scanf("%d/%d/%d",&dd,&mm,&yy);
//check year
if(yy>=1900 && yy<=9999)
{
//check month
if(mm>=1 && mm<=12)
{
//check days
if((dd>=1 && dd<=31) && (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12))
printf("Date is valid.\n");
else if((dd>=1 && dd<=30) && (mm==4 || mm==6 || mm==9 || mm==11))
printf("Date is valid.\n");
else if((dd>=1 && dd<=28) && (mm==2))
printf("Date is valid.\n");
else if(dd==29 && mm==2 && (yy%400==0 ||(yy%4==0 && yy%100!=0)))
printf("Date is valid.\n");
else
printf("Day is invalid.\n");
}
else
{
printf("Month is not valid.\n");
}
}
else
{
printf("Year is not valid.\n");
}
return 0;    
}
output:-
Enter date (DD/MM/YYYY format): 22/02/2000
Date is valid.

note:- Any validation request on comment box 

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post