954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need help..want to input only alphabets

i wrote this program and i want display 0, if the character entered is not an alphabet (!@#$% 5 or any other non alphabet)

can i have a quick solution

#include
void main ()
{
char char1,char2;
printf("\tEnter first character :");
scanf("\t%c", &char1);
printf("\tEnter second character :");
scanf("\n\t%c", &char2);

while (char2 < char1)
{
printf("\t!!!User must enter second character before the first\n");
printf("\tPlease enter valid second character :");
scanf("\n\t%c", &char2);
}

printf("\n\tNumerical Values Between %c and %c\n",char1,char2);
printf("\t................................\n");
printf("\n\tChar\tDecimal\tOctal\tHex\n");
printf("\t=============================\n");

while (char1 <= char2)
{
printf("\t%c\t%d\t%o\t%x\n",char1,char1,char1,char1);
char1++;
}
getch();
}

nhamyl
Newbie Poster
7 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This code might help you:

char c;
c = 's';
if(('a'<=c && c<= 'z') || ('A'<=c && c<= 'Z'))
{
     /* The character is an alphabetic character */
}
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

I am not sure what exactly you want to do but, if I understand properly, you want to make sure that the char1 and char2 are always letters('a' to 'z' or 'A' to 'Z')?

If that is what you want then all you need is a while loop.

while(1)
{
      cout << "Enter the first character" << endl;
      cin >> char1;
      if((char1 >= 48 && char1 <= 97) || (char1 >= 65 && char1 <= 90))
     {
             goto here;
     }
}

here:

As you can see, it will keep repeating the while loop until the character is a letter. When it is a letter, it will go to the 'here:' which should be just after the while loop.

djextreme5
Junior Poster
104 posts since Mar 2009
Reputation Points: 76
Solved Threads: 6
 

That's looks like the horrible version of what Tux4life already posted... Read this (by clicking it) --> goto.png

Attachments goto.png 25.6KB
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

hey that not wot i need
bt thanx for ur kind reply

here is ma question...i have done this program n cannot run n also want to add one function

#include
#include


int main()
{
char input;

printf("Enter a character");
scanf("%s", &input);

if ( input >= 'A' && input<= 'Z' )
printf("The lower case equivalent is %s\n", static_cast ( input + 'a' - 'A' ) );
else if ( input>= 'a' && input <= 'z' )
printf("The upper case equivalent is %s\n", static_cast ( input + 'A' - 'a' ));
else
printf("The character is not a letter");

}

here the problem is num and special characters also accepting.. i want the program only accept alphabets and If the character entered is not an alphabet (!@#$% 5 or any other non alphabet), i want display 0

nhamyl
Newbie Poster
7 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

you just need to add an if condition in the above program but u must need to know the ASCII range in which all those alphabets fall.

jawaad17
Newbie Poster
4 posts since Apr 2009
Reputation Points: 10
Solved Threads: 1
 

can any one do that pllllz help meeeeee

nhamyl
Newbie Poster
7 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 
If the character entered is not an alphabet (!@#$% 5 or any other non alphabet), i want display 0

> I already posted a piece of code which evaluates wheter the character is an alphabet character or not ...can any one do that pllllz help meeeeee > Sure we can do that for you, but it's your homework ...you just need to add an if condition in the above program but u must need to know the ASCII range in which all those alphabets fall. > What do you think the following does? #if(('a'<=c && c<= 'z') || ('A'<=c && c<= 'Z'))

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Hey,

I think there's a function " bool isalpha(char) " in that can preform the check for you.

Regards.

ahlamjsf
Newbie Poster
7 posts since Apr 2009
Reputation Points: 19
Solved Threads: 0
 

Why not just use isalpha ?

[edit] Sorry I didn't saw ahlamjsf's solution.
It was so that I had been amazed (shocked) by looking already posted pathetic codes above.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

here i done #include
#include
#include

int main()
{
char ch;

printf("Enter a character: ");
scanf("%c", &ch

if( isalpha ( ch ) ) {
if ( isupper ( ch ) ) {
ch = tolower ( ch );

printf("The lower case equivalent is %c\n",);
}
else {
ch = toupper ( ch );

printf("The upper case equivalent is %c\n", );
}
}
else
printf("The character is not a letter");

cin.get();
}


but cannot run ... the error "is comparing signed and unsigned values"
help me to slove this plzzz

nhamyl
Newbie Poster
7 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

#include should be
#include

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

here i done #include #include #include

int main() { char ch;

printf("Enter a character: "); scanf("%c", &ch

if( isalpha ( ch ) ) { if ( isupper ( ch ) ) { ch = tolower ( ch );

printf("The lower case equivalent is %c\n",); } else { ch = toupper ( ch );

printf("The upper case equivalent is %c\n", ); } } else printf("The character is not a letter");

cin.get(); }

but cannot run ... the error "is comparing signed and unsigned values" help me to slove this plzzz

Hey,

It would be helpful if you put your code in the code tags and properly indent it.

But from what I can see, you are missing a few brakets and semicolons...

djextreme5
Junior Poster
104 posts since Mar 2009
Reputation Points: 76
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You