-
i need help in sorting
i'm writing a program that can sort strings in alphabetical order, and i'm having trouble with the validation of input. My program sorts the strings just fine, but it should NOT accept any numeric values from the user.
can someone help me with the syntax for this validation? (should not accept any numeric values) All i need is the syntax or a sample code. tnx.:)
-
Well, it sounds like you need help with input validation instead of sorting ;)
One way to do this is to check if the string that has been read contains a digit, perhaps by using isdigit().
-
Code:
char line[100];
if (scanf("\t%99[a-zA-Z]\t", line) != 1)
return 1;
it will eat all space chars and take all letters
" 1234" - exit
" abcd1234" - take abcd
also you can get an all string and then do strpbrk for numbers
Code:
if (strpbrk(line, "0123456789") != NULL)
return 1;
but you will get punctuation like it is valid (id est if you need punctuation it will be ok and if not so ...)
if you want most reliable check, you would use isalpha() (because it will exclude all other chars, unless a-zA-Z)
Code:
#include <string.h>
/* OnlyLettersLine: check line l for letters only */
int OnlyLettersLine(const char *l)
{
if (l == NULL || *l == '\0')
return EOF;
while (*l != '\0')
if (!isalpha(*l++))
return 0;
return 1;
}
if (OnlyLettersLine(line) != 1)
return 1;
example for this function