Click here to Skip to main content
15,891,204 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,x,y,j,damn;
    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(b=0; b<13; b++)
    {
        while(i<13)
        {
            compare(ap,bp);
            i++;
        }
        for(y=0; y<13; y++)
        {
            printf("%s\n", info[y]);
        }
        return 0;
    }
}

void compare(char *ap,char *bp)
{
    int i=0;
    int x;
    int damn;
    if(*ap==*bp)
    {
        int j=1;
        while (*(ap+j)==*(bp+j))
        {
            if (*(ap+j) == '\0' && *(bp+j) == '\0')
            {
                damn=0;
                break;
            }
            j++;
        }
        if (*(ap+j)<*(bp+j))
        {
            damn=1;
        }
        if (*(ap+j)>*(bp+j))
        {
            damn=-1;
        }
    }
        if (*ap<*bp)
        {
            damn=1;
        }
        if (*ap>*bp)
        {
            damn=-1;
        }
        if (damn==-1)
        {
            while(*(ap+x) != '\0' || *(bp+x) != '\0')
            {
                swap(ap,bp);
                x++;
            }
        }
}

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


What I have tried:

I change some part and make a new function compare but it didn't work.
Posted
Updated 19-Dec-17 1:08am
v3

The correct implementation of your swap function would be
C
void swap( char pa[], char pb[], int size)
{
  char t;
  int n;
  for (n=0; n<size; ++n)
  {
    t = pa[n];
    pa[n] = pb[n];
    pb[n] = t;
  }
}

Please note: copying character arrays is very inefficient compared to just swap character pointers (the option suggested at your previous post).
 
Share this answer
 
Comments
victor hsu 1B 19-Dec-17 3:28am    
i change it but it still can't work.
Your info is a two dimensional array of chars where info[index] accesses an array of NULL terminated strings of type char[] resp. char*.

These are commonly called string pointers. When comparing these uing the == operator, you compare the pointers (the addresses). To compare the content (the strings), there is a C standard library function: strcmp - C++ Reference[^].

If you want to access characters from your info, you have to use two indexes: info[string_index][char_index].

When ap is pointing to one of the strings in info, you can also use ap[char_index] or *(ap + char_index). I suggest to use the first method because it is not so obscure as the second. The second from is used when there is no char index (just *ap); e.g. when incrementing the pointer within a loop.

So use strcmp to compare strings or do it in your own code by accessing characters as described above.
 
Share this answer
 
Comments
victor hsu 1B 19-Dec-17 4:00am    
okay but our teacher don't allow us to use library function strcmp
Jochen Arndt 19-Dec-17 4:07am    
Then you have to use char access as described in my solution.

You are doing it wrong here for example:
while ((ap+j)==(bp+j))

It must be
ap[j] == bp[j]

or
*(ap+j) == *(bp+j)
victor hsu 1B 19-Dec-17 4:10am    
okay and now it still didn't swap? why
Jochen Arndt 19-Dec-17 4:30am    
That has been already answered by solution 1 and 3.

However, there may be also more errors in your code (which might be also logic errors meaning that it is correct code but not doing it the way that is intended).

I suggest to move the string comparison to an own function (that is: write your own strcmp). Then the main() code becomes shorter and logic errors are more obvious.

Hint:
Have a look at where you call the swap function.
What is the purpose of the while loop?
What is the value of x initially?
What is the value of x with the next loop iteration?
C is zero based, it mean that in an array of 20 elements, their positions are from 0 to 19.
C++
void swap(char *ap, char *bp)
{
    printf("swap");
    char tmp[20];
    int x;
    for(x=0;x<=19;x++)
          {
               tmp[20]=*(ap+x);
               *(ap+x)=*(bp+x);
               *(bp+x)=tmp[20];
          }
}

tmp[20] is outside of array tmp.
Note that tmp do not need to be an array, a single char is enough, as outlined in solution 1.

You really need to learn debugger to help you learn what works and what don't.

[Update]
Quote:
someone say my swap have problem ,but i don't know which part have problem can u help me.

I don't know what is the exact problem, there was many versions of your code with many corrections suggested.

By learning programming with C, you are doing it the hard way because with C you have to handle all the details.
My advice: Learn to program with Visual Basic, it will handle most of details and you can concentrate on learning programming.
Once you know programming, you can learn specifics of C.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
 
Share this answer
 
v4
Comments
victor hsu 1B 19-Dec-17 3:59am    
i run it but error:subscripted value is neither array nor pointer nor vector, i don't understand what it mean
Patrice T 19-Dec-17 4:20am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Show actual code, error message and line number
victor hsu 1B 19-Dec-17 9:02am    
someone say my swap have problem ,but i don't know which part have problem can u help me.

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