check if 'string' has alphabet, number and alphabet upper case at once

0 Hayden Kuk · October 30, 2015

int main()
{
  char a = ' ';
   printf("input :");
   scanf(" %s", &a);

   if(isdigit(a) && isalpha(a) && isupper(a))
   {
       printf("valid");
   }
   else
   {
       printf("invalid");
   };
}


this is my code. I want if statement to check if input 'a' has all of three elements. I posted this 2 days ago and someone replied, saying that I should change the variable int to char making it a string, but yet it does not work. the result is always invalid... why?
doesn't char include numbers and alphabets together?

Post a Reply

Replies

Oldest  Newest  Rating
+1 arvind Venkatesh · October 31, 2015
The result was always invalid because according to your code result will be vaild if isdigit(a), isalpha(a) and isupper(a) are all true..means your code result will be "valid" if  a character is alphabet and also a number..which is not possible so you always get invalid..




#include<stdio.h>
#include<strinng.h>

int main()
{
char a[100];//concept of array
printf("input :");
scanf(" %s", &a); //alternately you can use gets(a);

int lengthOfString = strlen(a);//find out length of string
int conNum = 0;//this is currently set 0 but will become 1 if we find a digit...
int conAlpha = 0;//same as above , this will become 1 if we find a alphabet
int conUpper = 0;
int i;

for(i=0;i<lengthOfString;i++)
{
 if(isalpha(a)
 {
    conAlpha = 1;//if there is a alphabet at i'th position then we make it 1..
  }
  if(isdigit(a)
 {
    conNum = 1;
 }
  if(isupper(a)
      {
           conUpper = 1;
     }
}


if(conNum == 1 && conAlpha == 1 && conUpper == 1)//if all are one that means we found upper case, alphabet and a number
{
printf("valid");
}
else
{
printf("invalid");
} //never-ever use semi-colon here

return 0;
}

hope this helped...:O:angel::'(:D
  • 1

C

106,955 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator