hi can anyone please help me
with this problem
arranging given input that is alphanumeric in ascending and descending order
using turbo c

*note the given info are char only

example output:
info to be arranged: blk4 lot 32

ascending order: bkllot234
descending order:tollkb432

Digits sort lower than any letters, so ascending order would start with 234bk... etc. Descending order is OK.

Normally, you'd want a separate function for sorting, but C also has qsort() as a very capable sorter.

Post up some code so we can see how the program is organized.

    this is a program i had processed but still has errors for descending order but i still dont have any idea why it errors and it still doesnt have ability to sort numbers




    #include<stdio.h>
    #include<ctype.h>
    #include<conio.h>

    void main()
        {


    int i,j;
    char text[30];
    clrscr();
    printf("Enter text:");
    gets(text);
    clrscr();
    printf("Ascending:\n");
        for(i=65;i<=90;i++)
        {
        for(j=0;j<30;j++)
        {
        if(text[j]==toupper(i)||text[j]==tolower(i))
        printf("%c",text[j]);
        }
        }

    printf("Descending:\n");
        for(j=65;j<=90;j++)
        {
        for(i=0;i<30;i++)
        {
        if(text[i]==toupper(j)||text[i]==tolower(j))
        printf("%c",text[i]);
        }
        }
     getch();
        }


    {
    for(j=0;j<30;j++)
    {
    if(text[j]==toupper(i)||text[j]==tolower(i))
    printf("%c",text[j]);
    }
    }
 getch();
 }

i still get confused on this but i know im just a student on this and want to really learn please please help me

Just because you have Turbo C doesn't mean you have to use stupid Turbo Cisms. Anyway, the C library already provides everything you need, most notably qsort() for sorting arrays.

On the assumption that the examples you provided are what you really want (with digits always being last), and that you desperately need examples of good code, here's one of my rare complete solutions:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int compare_ascending(const void *a, const void *b)
{
    return *(const char*)a - *(const char*)b;
}

static int compare_descending(const void *a, const void *b)
{
    return *(const char*)b - *(const char*)a;
}

int main(void)
{
    char buf[BUFSIZ];

    printf("Enter text: ");
    fflush(stdout);

    if (fgets(buf, sizeof buf, stdin)) {
        int direction;

        printf("Ascending(1) or descending(2)? ");
        fflush(stdout);

        if (scanf("%d", &direction) == 1) {
            char *p;

            buf[strcspn(buf, "\n")] = '\0';

            /* Assume ascending for unrecognized values */
            if (direction != 2) {
                char temp[BUFSIZ] = {0};

                qsort(buf, strlen(buf), 1, &compare_ascending);

                /* Trim leading whitespace */
                p = buf + strspn(buf, " \t\n\r\f\v");
                memmove(buf, p, strlen(p) + 1);

                /* Rotate numbers to the end */
                p = buf + strspn(buf, "0123456789");
                strncat(temp, buf, p - buf);
                memmove(buf, p, strlen(p) + 1);
                strcat(buf, temp);
            }
            else {
                qsort(buf, strlen(buf), 1, &compare_descending);

                /* Trim trailing whitespace */
                p = buf + strlen(buf);

                while (isspace(*p)) {
                    --p;
                }

                *p = '\0';

                /* Rotation not needed for descending */
            }

            printf("Sorted result: '%s'\n", buf);
        }
    }

    return 0;
}

Please, please ask questions and learn from that example. If you shoot back with "it's too complicated", or "my teacher won't allow it" then I'll be upset.

Are you supposed to code up the sorter, or is using qsort() OK?

well thats the best thing as long as my output is the same as what he wants and i cud somehow explian to him the output or how i get the output im fine.. somehow the reason why i posted the prob here is coz he aint really teaching my class anything about using the functions or what its use is i want to learn and i want to be able to understand the functions of this problem...

@decepticon... thank you for the code i really appreciate this... the thing is can you explain to me those codes please i really wanna learn...

@adak the sorter is ok as long as the output that my prof wants is what we get as an output..

ow one more thing to add how do i code in paramiters if special characters are placed it would be invalid....

sorry guys im really a newbie on this i get the really basic codes but the others not so much but i wanna learn and i really thank those who are kind enough to help

the thing is can you explain to me those codes please i really wanna learn...

I'll be happy to explain anything you want, but there's too much to explain everything possible from top to bottom. Please specify areas that are confusing you and mention why you're confused. That gives me a place to focus on in my explanations.

most of your code we havent tackled yet
firstly what does void do and when do i use it same with static int??
i havent tried it on my tc yet the type of tc we use is dosbox0.74
so when i try use isostream it wont take it i keep seeing this when search for already preped codes on the internet but all i get is errors so then i get confused.... sorry if i got alot alot of questions since i dont really trust my profesors way of teaching all he does is give outputs then we just design the program so its like i learn on my own without knowing how or when to use a code all i understood was the stdio.h and conio.h then char, int, gets, getch, and clrscr... when i use return my prof doesnt want it.... and is goto a command i could use im lost here so really do apologize if i may ask crazy questions... hope you bare with me please

most of your code we havent tackled yet

Get used to that feeling. If you're not confused 90% of the time, you're not growing as a programmer.

firstly what does void do and when do i use it same with static int??

void does two things:

  1. It represents "nothing". So if you're returning nothing from a function, the return type would be void. If you're accepting no parameters to a function, the parameter list would be void. If you're ignoring a return value and the compiler complains, you can cast it to void. For example:

    (void)getchar(); // Read a character and ignore it
    
  2. In the context of a pointer, void* is C's concept of a generic pointer that can point to any object.

As concerns static int, those two keywords do different things. The return type of the function is int, and the function itself is qualified as static to give it visibility only to the file it's defined in. If you don't qualify functions as static, they're extern by default, which means that any other file can call the function. For the most part you can simply not worry about that behavior until later. I made those two functions static as a matter of good style because they're only intended to be used in a single file.

so when i try use isostream it wont take it

iostream is C++, not C.

sorry if i got alot alot of questions since i dont really trust my profesors way of teaching all he does is give outputs then we just design the program so its like i learn on my own without knowing how or when to use a code all i understood was the stdio.h and conio.h then char, int, gets, getch, and clrscr... when i use return my prof doesnt want it.... and is goto a command i could use im lost here so really do apologize if i may ask crazy questions... hope you bare with me please

That was a stream of consciousness that I'm not sure contains any actual questions.

so void may ignor or b a pointer?

BUFSIZ what does it do? same with fflush(stdout)

so void may ignor or b a pointer?

void is a type, just like int is a type. However, void is a very restricted type in that you can't do very much with it.

BUFSIZ what does it do?

BUFSIZ is a macro defined in stdio.h that represents the size of default stream buffer for things like stdin and stdout. It's a convenient value for input buffers in user code as well, and it usually expands to something like 512 or 1024. So given the assumption that BUSIZ is defined like this:

#define BUFSIZ 512

We can then say that char buf[BUFSIZ]; is equivalent to char buf[512];.

same with fflush(stdout)

fflush() forces output streams to write buffered characters to the destination. This can be confusing because the buffering is well hidden, but when you say printf("Enter text: "); there's no guarantee at all that the text show up on your screen immediately because printf() isn't required to flush the buffer. There are only two cases where you can absolutely control whether the buffer is flushed:

  1. When you send '\n' to the stream. If the printf() were changed this printf("Enter text:\n"); then stdout would be flushed automatically, but that would make the output less attractice because then the prompt and input would be on separate lines.

  2. When you call fflush(). Note that fflush() is only defined for output streams. You're almost guaranteed to see things like fflush(stdin), but those are wrong because stdin is an input stream.

The buffer is also flushed when it fills up, but you have no control over that. Some libraries will also flush the buffer at other times to make life easier, but relying on that makes your code non-portable.

uhm desceptikon you mind i ask where i can put a clrscr command on your codes and after i input 1 / 2 it goes back to the code screen and i had to run it again to see the result of the arrange is there a way to have it stay on the run preview as it arranges the format of characters?? i tried to put clrscr on the usual area we put it but it errors out

you mind i ask where i can put a clrscr command on your codes

How about nowhere? clrscr() is a Turbo Cism, it basically makes your code non-portable for no good reason at all. In fact, you should avoid the entire conio.h library whenever possible.

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.