LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-22-2005, 01:39 AM   #1
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Rep: Reputation: 15
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.

Last edited by kponenation; 11-22-2005 at 01:53 AM.
 
Old 11-22-2005, 02:00 AM   #2
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
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.
 
Old 11-22-2005, 02:03 AM   #3
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
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.
 
Old 11-22-2005, 05:12 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
What immediately catches the eye:
Code:
if (result = 0)
This is not a compare but an assign (so result will be set to zero)
 
Old 11-22-2005, 07:42 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.

Last edited by dmail; 11-22-2005 at 07:44 AM.
 
Old 11-22-2005, 09:49 AM   #6
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
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.
 
Old 11-22-2005, 10:12 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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).

Last edited by Wim Sturkenboom; 11-22-2005 at 10:15 AM.
 
Old 11-22-2005, 10:17 AM   #8
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
thanks Wim,
so i changed it to result ==0
but now it won't even go into the while loop...
 
Old 11-22-2005, 10:46 AM   #9
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
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;
}
 
Old 11-22-2005, 11:19 AM   #10
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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?

Last edited by dmail; 11-22-2005 at 11:23 AM.
 
Old 11-22-2005, 11:25 AM   #11
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
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.
 
Old 11-22-2005, 11:32 AM   #12
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
 
Old 11-22-2005, 11:40 AM   #13
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
then how can i get the result i want?
thanks.
 
Old 11-22-2005, 11:54 AM   #14
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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]);
 
Old 11-22-2005, 12:06 PM   #15
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
c++ strings-strcmp function...... sachitha Programming 4 09-12-2004 07:28 AM
strcmp and some problems I have [strange ones] zeppelin Programming 8 04-03-2004 06:02 PM
How to compare these two strings in one line code? powerplane Programming 4 07-10-2003 12:09 AM
strcmp function does not seemed to work Linh Programming 4 06-12-2003 06:09 AM


All times are GMT -5. The time now is 08:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration