i need help in sorting

This is a discussion on i need help in sorting within the C Programming forums, part of the General Programming Boards category; i'm writing a program that can sort strings in alphabetical order, and i'm having trouble with the validation of input. ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    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().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21