Q.Write a function to accept 20 characters and display whether each character input is a digit, lower case letter or upper case letter.

Solution I tried

#include<stdio.h>
#include<conio.h>
int main()
{
char inp;
int i;
clrscr();
for(i=0;i<20;i++)
 {
        printf("Enter a character\n");
 scanf("%c",&inp);
 if(inp>='0'&& inp<='9')
  {
  printf("It is a digit\n");
  }
        else if(inp>='A'&& inp<='Z')
        {
        printf("It is upper case letter\n");
        }
        else if(inp>='a'&& inp<='z')
  {
  printf("It is lower case letter\n");
  }
       }
return 0;
}

But the problem is that when i run it, the comment "enter a character " appears twice. So instead of 20 characters, it takes only 10. Please help!!!

hey,
I copied your code to see if i could do something about it, you know what i found out?......
the compiler is built in such a way that whenever you type a wrong input, for instance you declared a char and you input an int; it executes the program without any further request for input, it will just print all the "printfs" and exits the program...That's what happened when you input 0 to 9, you declared char and not integers, if you do test for characters alone, you will see you are not bad.. You can verify what i am saying by writing a program to input two integers and calculate their sum... If you try to input a character as the first number, the compiler wont ask you for the second number and it will bring out a garbage value.. e.g -8003745372
about your program,
you know all you want is input 20 characters and determine which is digit, uppercase and lower case, lets try to using strings , you know a string can contain both digits and integers, although i have not figured out how to but i will try to do something about it;
declare the var inp as string;
when you want to test you can try something like
if(inp >= "A" && inp <="Z"){.......}
mind the double quote for strings

av a nice day

Thanx for the help but the problem is my complier doesn't do too well with getchar and gets. I tried doing that but it didn't work.

Thanx for the help but the problem is my complier doesn't do too well with getchar and gets.

Your compiler works fine with getchar() and no one should ever use gets() . Please don't blame the compiler because you haven't learned all there is to know about how input works. It takes time and experience. You have to keep in mind that even the ENTER key is a character.

I tried doing that but it didn't work.

No enough of an explanation. Can't help you fix it without details.

Your compiler works fine with getchar() and no one should ever use gets() . Please don't blame the compiler because you haven't learned all there is to know about how input works. It takes time and experience. You have to keep in mind that even the ENTER key is a character.


No enough of an explanation. Can't help you fix it without details.

Which other fucntion can I use instead of gets to accomodate a string with spaces? And when I use getchar() in my compiler, it does not accept the input and so does not proceed with the code.

Which other fucntion can I use instead of gets to accomodate a string with spaces? And when I use getchar() in my compiler, it does not accept the input and so does not proceed with the code.

You can use a loop to receive the string character by character...

For example:

for(i = 0; i < SIZE; i++){
scanf(" %c",array[i]);//You also use that array[i] = getchar();
}

Finally try to use the getchar(); which was showned above and mind out that scanf's usage above again...

This article has been dead for over six months. Start a new discussion instead.