LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [C]How to compare two strings w/o using strcmp() (https://www.linuxquestions.org/questions/programming-9/%5Bc%5Dhow-to-compare-two-strings-w-o-using-strcmp-385406/)

kponenation 11-22-2005 01:39 AM

Sorting a two string in C
 
The followings are my code

Code:

int i = 0;
    int tmpArray;
    int result;
    while (i < N-1){
          result = compare_words(word_array[i], word_array[i+1]);
          printf("%d",result);
          if (result = 0){
                tmpArray = *word_array[i];
                *word_array[i] = *word_array[i+1];
                *word_array[i+1] = tmpArray;
                  }         
    i = i + 1;     
          }

result =0 means word_array[i] >word_array[i+1]

how can i make this to work?

Thanks in advance.

primo 11-22-2005 02:00 AM

There's no primitive to do it in C as it's done by other languages. If there's a need to skip strcmp(), you should do it manually then.

kponenation 11-22-2005 02:03 AM

i figured out the comparing part ....
i need to know how to sort the words now...
i think my algorithm is correct (i already wrote this in Java)
but it doesn't work.

Wim Sturkenboom 11-22-2005 05:12 AM

What immediately catches the eye:
Code:

if (result = 0)
This is not a compare but an assign (so result will be set to zero)

dmail 11-22-2005 07:42 AM

Re: Sorting a two string in C
 
Quote:

Originally posted by kponenation
The followings are my code

Code:

int i = 0;
    int tmpArray;
    int result;
    while (i < N-1){
          result = compare_words(word_array[i], word_array[i+1]);
          printf("%d",result);
          if (result = 0){
                tmpArray = *word_array[i];
                *word_array[i] = *word_array[i+1];
                *word_array[i+1] = tmpArray;
                  }         
    i = i + 1;     
          }

result =0 means word_array[i] >word_array[i+1]

how can i make this to work?

Thanks in advance.

does this code really compile?

by the looks of the code word_array is an array of pointers to strings, is this correct?
but then this line confuses me
Code:

tmpArray = *word_array[i];
if word_array is an array of strings
then word_array[i] is a pointer to a string, then dereferencing it gives the first char in the array, and you put this in an int(which is the same size as a char);
it looks like you are trying to sort strings(whole words) but then you do
Code:

                *word_array[i] = *word_array[i+1];
                *word_array[i+1] = tmpArray;

maybe ive lost the plot, but maybe you could give some more info like what you are exactly are trying to do and include the declartion of the types.

kponenation 11-22-2005 09:49 AM

Thanks for your reply.
i'll just post the function sort()
Code:

int sort_words(char **word_array, int N ){ 
    int i = 0;
    int tmpArray;
    int result;
    while (i < N-1){
          result = compare_words(word_array[i], word_array[i+1]);
          printf("%d",result);
          if (result = 0){
                tmpArray = *word_array[i];
                *word_array[i] = *word_array[i+1];
                *word_array[i+1] = tmpArray;
                                  }         
    i = i + 1;     
          }
    return 0;
}

Above code did compile w/o any warnings or errors. word_array is a double pointer to a two dimentional array.
Thank you.

Wim Sturkenboom 11-22-2005 10:12 AM

as I said:
if(result = 0)
will assign 0 to result

and this will always evaluate to false and you will never do the swap

There might, btw, be a couple of other things wrong (did not look at it).

kponenation 11-22-2005 10:17 AM

thanks Wim,
so i changed it to result ==0
but now it won't even go into the while loop...

kponenation 11-22-2005 10:46 AM

I modified my code a little

Code:

int sort(char **word_array, int N){ 
    int i = 0;
    int j = 0;
    int tmpArray;
    int result;
    while (i < N){
          while(j < N-1){
          result = compare_words(word_array[j], word_array[j+1]);
          printf("%d",result);
          if (result == 0){
                tmpArray = *word_array[j];
                *word_array[j] = *word_array[j+1];
                *word_array[j+1] = tmpArray;
                }
                j = j +1;
                                  }         
    i = i + 1;     
          }
    return 0;
}


dmail 11-22-2005 11:19 AM

Quote:

Originally posted by kponenation
I modified my code a little

Code:

int sort(char **word_array, int N)

    int i = 0;
    int j = 0;
    int tmpArray;
    int result;
    while (i < N)
    {
          while(j < N-1)
          {
                result = compare_words(word_array[j], word_array[j+1]);
              printf("%d",result);
              if (result == 0)
              {
                  tmpArray = *word_array[j];
                  *word_array[j] = *word_array[j+1];
                  *word_array[j+1] = tmpArray;
              }
              j = j +1;
          }         
            i = i + 1;     
      }
    return 0;
}


ok i see what you are trying to do, but thes lines still confuse me.
Code:

                  tmpArray = *word_array[j];
                  *word_array[j] = *word_array[j+1];
                  *word_array[j+1] = tmpArray;

after the first line what do you think the value of tmpArray is?
why do you have two while loops? j is never reset so if the inner loop takes j to its max value the inner loop is exited and i is incremented but j is still its max value so the inner loop is not entered.
why does the func return and int?

kponenation 11-22-2005 11:25 AM

value of tmpArray is the address of word_array[j] right?
word_array[0] = program
word_array[1] = linux
so word_array[0][0] = p
word_array[0][1]= r
word_array[1][0]= l
word_array[1][2]= n
returning int doesn't really matter. i just need to re-arrange (sort) the array and save it.
When i run my program it stops after tmpArray = *word_array[j];
it won't run *word_array[j] = *word_array[j+1];
Why is that?
Thanks.

dmail 11-22-2005 11:32 AM

Quote:

value of tmpArray is the address of word_array[j] right?
i'll have to write a small test programme to validate this, but you are trying to store a hex address in an int, which is then cast to a char? this just looks very wrong to me and i would be very suprised if you get the outcome you wanted.

kponenation 11-22-2005 11:40 AM

then how can i get the result i want?
thanks.

dmail 11-22-2005 11:54 AM

Code:

        char* words[20];
        words[0]="program\0";
        words[1]="linux\0";
        //print before we swap;
        printf("word one:%s\n word two:%s\n",words[0],words[1]);
        //swap program and linux aroung
        char* temp;
        temp = words[0];
        words[0] = words[1];
        words[1] = temp;
        //print after we swap;
        printf("word one:%s\n word two:%s",words[0],words[1]);


kponenation 11-22-2005 12:06 PM

yeah... but the problem is i need to make the array two dimensional because i first need to compare the two strings (each character) to see which one comes before.
so i can't do it like what you did.


All times are GMT -5. The time now is 04:28 AM.