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 <stdio.h>
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();
}

Recommended Answers

All 12 Replies

This code might help you:

char c;
c = 's';
if(('a'<=c && c<= 'z') || ('A'<=c && c<= 'Z'))
{
     /* The character is an alphabetic character */
}

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.

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

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 <stdio.h>
#include <iostream.h>


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<char> ( input + 'a' - 'A' ) );
else if ( input>= 'a' && input <= 'z' )
printf("The upper case equivalent is %s\n", static_cast<char> ( 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

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.

can any one do that pllllz help meeeeee

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'))

Hey,

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

Regards.

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.

here i done #include <stdio.h>
#include <iostream>
#include <string>

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

#include <string> should be
#include <cstring>

here i done #include <stdio.h>
#include <iostream>
#include <string>

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.