Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>
#include <stdlib.h>

void swap(char *ap,char *bp);
void compare(char *ap,char *bp);
int main()
{
    int b,c,y;
    int i=0;
    char info[13][20]=
    {
        "christina",
        "victor",
        "chris",
        "chester",
        "elta",
        "kezia",
        "bew",
        "grace",
        "mavis",
        "tony",
        "oat",
        "adonique",
        "ploy"
    };
    char *ap;
    ap=info[i];
    char *bp;
    bp=info[i+1];
    for(i=0; i<13; i++)
    {
        printf("k");
        compare(ap,bp);

        i++;
    }
    for(y=0; y<13; y++)
    {
        printf("k");
        printf("%s\n",info[y]);
    }
    return 0;
}


void compare(char *ap,char *bp)
{
    printf("k");
    int x;
    int j=0;
    int damn;
    if(*ap==*bp)
    {
        while (*(ap+j)==*(bp+j))
        {
            if (*(ap+j) == '\0' || *(bp+j) == '\0')
            {
                break;
            }

        }
        if (*(ap+j)<*(bp+j) || *ap<*bp)
        {
            damn=1;
        }
        if (*(ap+j)>*(bp+j)||*ap>*bp)
        {
            while(*(ap+j) != '\0' || *(bp+j) != '\0')
            {
                swap(ap,bp);
                j++;
            }
        }
    }
}

void swap(char *ap,char *bp)
{
    int c;
    char tmp[20];
    for(c=0; c<19; c++)
    {
        printf("k");
        *tmp=*ap;
        *ap=*bp;
        *bp=*tmp;
    }
}


What I have tried:

i tried to debug print sth in function, like i print in compare function and it didn't print but i don't know how to do it pls help~~~~
Posted
Updated 19-Dec-17 22:40pm
Comments
Richard MacCutchan 20-Dec-17 4:20am    
Get yourself a proper book on C and study it. You will not learn programming properly by posting questions here.

You were told yesterday: use the debugger.
Why my program can't swap (help~~~~~)[^]

Look at what is happening to your variables while the code is running.
But that is odd code ... why are you incrementing loop variables twice?
for(i=0; i<13; i++)
{
    printf("k");
    compare(ap,bp);

    i++;
}
 
Share this answer
 
Comments
victor hsu 1B 24-Dec-17 22:03pm    
oh i++
Don't you think you missed something here:
char *ap;
ap=info[i];
char *bp;
bp=info[i+1];
for(i=0; i<13; i++)
{
    printf("k");
    compare(ap,bp);
     
    i++;
}
You are doing the same operation on the same data 13 times. You have to move the assignments for ap and bp into the loop.
 
Share this answer
 
Comments
victor hsu 1B 24-Dec-17 22:02pm    
so i have to ap++ and bp++ right?
Jochen Arndt 25-Dec-17 3:49am    
Think about it.

Incrementing a char* pointer will let it point to the next character of the string. But you have to call compare for the next strings of your info[] array. So you have to increment the index for that array which is already done because it is the loop variable. All you need is to use that variable which can be done by moving the assignments for ap and bp into the loop.
I suggest you to 'divide and conquer': Use two functions, one for string comparison and another one for sorting:
C
int compare( const char * sl, const char * sr)
{
  //implementation left to the OP
}

void sort( const char * info[], int items)
{
  // implementation left to the OP
  // uses the compare and swap functions
}
 
Share this answer
 
Comments
victor hsu 1B 24-Dec-17 22:03pm    
you mean separate to two function?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900