C program to test case of character

Reply

Join Date: Jun 2009
Posts: 28
Reputation: seo2005 is an unknown quantity at this point 
Solved Threads: 0
seo2005 seo2005 is offline Offline
Light Poster

C program to test case of character

 
0
  #1
32 Days Ago
I want to write a C program to test whether the character is uppercase, lowercase, digit or special symbol.
  1. void main ()
  2. {
  3.  
  4. char ch;
  5.  
  6. printf("\n Enter any character from the keyborad");
  7.  
  8. scanf("%c",&ch);
  9.  
  10. if (ch>=65&&ch<=90)
  11.  
  12. printf(" \n the character is uppercase);
  13.  
  14. if(ch>=91&&ch>=122)
  15.  
  16. printf(" \n the character is lowercase);
  17.  
  18. if(ch>=48&&ch<=57)
  19. printf("\n You entered a digit );
  20.  
  21.  
  22.  
  23. }
  24.  

My question how to print whether the character is a special symbol. I don't know this. Please can anyone point out.

Thanks
Last edited by WaltP; 31 Days Ago at 3:11 am. Reason: Added CODE tags -- with all the help about them, how could you miss using them????
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: SecurExpert is an unknown quantity at this point 
Solved Threads: 1
SecurExpert SecurExpert is offline Offline
Newbie Poster
 
0
  #2
32 Days Ago
Since you are able to test for the first three, i think an else condition will be able to give you the fourth case, i.e a special character. Use your program to try this algorithm
  1. if(element_is_a_lowercase)
  2. print("lowercase")
  3. else if(element_is_an_uppercase)
  4. print("uppercase")
  5. else if(element_is_a_digit)
  6. print("digit")
  7. else
  8. print("is special")
hope it works!

Regards
Last edited by SecurExpert; 32 Days Ago at 3:14 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: seo2005 is an unknown quantity at this point 
Solved Threads: 0
seo2005 seo2005 is offline Offline
Light Poster

C program to test case of character

 
0
  #3
32 Days Ago
Originally Posted by SecurExpert View Post
Since you are able to test for the first three, i think an else condition will be able to give you the fourth case, i.e a special character. Use your program to try this algorithm
  1. if(element_is_a_lowercase)
  2. print("lowercase")
  3. else if(element_is_an_uppercase)
  4. print("uppercase")
  5. else if(element_is_a_digit)
  6. print("digit")
  7. else
  8. print("is special")
hope it works!

Regards

Thanks for this explanation. I would like to ask

1. Whether I can use ASCII code for to print special symbols, like I have done above in 3 cases. If so, would you please tell the codes.

2. Are there braces missing in my program after if statement.

3. Can I test this program online.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training
 
0
  #4
32 Days Ago
Originally Posted by seo2005 View Post
I want to write a C program to test whether the character is uppercase, lowercase, digit or special symbol.

void main ()
{

char ch;

printf("\n Enter any character from the keyborad");

scanf("%c",&ch);

if (ch>=65&&ch<=90)

printf(" \n the character is uppercase);

if(ch>=91&&ch>=122)

printf(" \n the character is lowercase);

if(ch>=48&&ch<=57)
printf("\n You entered a digit );



}

My question how to print whether the character is a special symbol. I don't know this. Please can anyone point out.

Thanks
This code is specific to the ASCII character set. It would not work with other character sets. There are functions available such as isupper(), islower(), isdigit() etc that you can use for this exercise. These functions are available with the ctype.h header file.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #5
32 Days Ago
1. Whether I can use ASCII code for to print special symbols, like I have done above in 3 cases.
What are special characters? Are they the glyph characters that are neither digits nor latin alphabet characters? Are they control characters? What about extended characters? All of the above? You need to specify what you think a special character is before you can test for one.

2. Are there braces missing in my program after if statement.
That is something your compiler will tell you.

3. Can I test this program online.
I do not know of a way to compile and run a program online, but you can compile your code here. Be sure to set it to compile as C though. It defaults to C++.

This code is specific to the ASCII character set. It would not work with other character sets.
Which ones? The only one I know of is EBCDIC, and EBCDIC is not common. Everything else that supports the latin characters is compatible with 7 bit ASCII, AFAIK.

For readability I would use character constants instead of straight values:
  1. if (ch >= 'A' && ch <= 'Z')
  2. {
  3. printf("The character is uppercase\n");
  4. }
  5. else if (ch >= 'a' && ch >= 'z')
  6. {
  7. printf("The character is lowercase\n");
  8. }
  9. else if (ch >= '0' && ch <= '9')
  10. {
  11. printf("You entered a digit\n");
  12. }
It is a safe assumption that this is a homework assignment and the is*() functions are not allowed or have not been covered yet. So it is fine to say they exist and recommend them, but you should also offer an alternative that matches the level of the program. Having to worry about portability, internationalization, and all of that junk so soon will just scare people off.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC