LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 12:40 PM   #16
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled

ok then try it this way
Code:
	const int max_size = 20;
	char words[2][max_size] = {"program", "linux"};
	//print before we swap;
	printf("word one:%s\nword two:%s\n",words[0],words[1]);	
	char temp[max_size];
	strcpy(temp,words[0]);
	strcpy(words[0],words[1]);
	strcpy(words[1],temp);
	//print after we swap;
	printf("word one:%s\nword two:%s",words[0],words[1]);
 
Old 11-22-2005, 12:42 PM   #17
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
thank you dmail for your help but i can't include string.h so i can't use strcpy()
i know it would be a lot easier if i can but i just can't...
 
Old 11-22-2005, 12:46 PM   #18
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally posted by kponenation
thank you dmail for your help but i can't include string.h so i can't use strcpy()
i know it would be a lot easier if i can but i just can't...
really? why cant you include "string.h"? the header is a standard c header isn't it?
in that case you cant use memcpy either.

looking at your earlier code
Code:
*word_array[i] = *word_array[i+1];
what this does is copy one char across and if you cant use string.h you may have to do this with 2d array for the lenght of the second index, to copy fully the string over.
another option is to create a struct string which looks something like

typedef struct tag_string
{
char* string;
int length;
}string,*p_string;

Last edited by dmail; 11-22-2005 at 12:50 PM.
 
Old 11-22-2005, 01:00 PM   #19
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
i modified it like this
[CODE]int sort(char **word_array, int N ){
int i = 0;
int j = 0;
char *tmpArray[width];
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[j]= word_array[j];
word_array[j] = word_array[j+1];
word_array[j+1] = tmpArray[j];
}
j = j +1;}
i = i + 1;}
return 0;
}

i tested w/ 5 words
word_array[0] = sad
word_array[1] = toy
word_array[2] = doom
word_array[3] = send
word_array[4] = cotton

my result is
sad
doom
send
cotton
toy
 
Old 11-22-2005, 02:54 PM   #20
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
This seems like homework. Oh well. If you can't USE strcmp, then WRITE strcmp so you can get on with it.
Code:
#include <stdlib.h>
typedef unsigned char uchar_t;

int
mystrcmp (p1, p2)
     void *p1;
     void *p2;
{
  uchar_t *s1 = (uchar_t *) p1;
  uchar_t *s2 = (uchar_t *) p2;
  uchar_t c1, c2;

  do
    {
      c1 = *s1++;
      c2 = *s2++;
      if (c1 == '\0')
	      break;
    } while (c1 == c2);

  return c1 - c2;
}

int compar(const void *a,const  void *b)
{
	return mystrcmp(a,b);
}

int main(int argc, char *argv[])
{
	char word[8][8]={
	         "dog"  ,
	         "cat"  ,
	         "house",
	         "child",
	         "zoo"  ,
	         "quick",
	         "animal",
	         "waste" };
	int i=0;
		
    qsort(word,8,8,compar);	          
	for(i=0;i<8;i++) printf("%s\n",word[i]);	          
    return 0;
}
 
Old 11-22-2005, 02:56 PM   #21
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
I know and i fixed it myself....
Thanks anyway for your help.
 
Old 11-23-2005, 02:57 AM   #22
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
Hai buddy--

What the hell is going on here?
If I've understood well what u r talking about then the problem is just a piece of cake. My code:
Code:
#include<stdio.h>
#include<string.h>
#include<malloc.h>

#define MAXLEN 20
#define STRINGS 5

int compStrings(char *x,char *y)
{
	unsigned int i;
	for(i=0;i<strlen(x) && i<strlen(y);i++)
	{
		if(x[i]>y[i])
			return 0;
		else if(x[i]<y[i])
			return 1;
	}
	if(i==strlen(x))
		return 1;
	return 0;
}

void sort(char **words)
{
	char *temp;
	int i=0,j,result;
	while(i<STRINGS)
	{
		j=i+1;
		while(j<STRINGS)
		{
			result=compStrings(words[i],words[j]);
			printf("%s %s result=%d",words[i],words[j],result);
			if(result==0)  /* words[i]>words[j] */
			{
				temp=words[i];
				words[i]=words[j];
				words[j]=temp;
			}
			j++;
		}
		i++;
	}
}

void main()
{
    char **WordList;
	int i;

	WordList=(char **) malloc(sizeof(char)*STRINGS);
	for(i=0;i<STRINGS;i++) 
	{
		WordList[i]=(char *) malloc(sizeof(char)*MAXLEN);
		printf("Enter the string %d",i);
		scanf("%s",WordList[i]);
	}

	sort(WordList);

	for(i=0;i<STRINGS;i++) 
		printf("\n %s",WordList[i]);	
}
The algorithm is not efficient. May be u can resort to quick and other types of sort if u need.

Send comments about the code

Cheers

Last edited by vivekr; 11-23-2005 at 03:01 AM.
 
Old 11-23-2005, 08:29 AM   #23
kponenation
Member
 
Registered: Jul 2004
Location: New Orleans, Louisiana
Distribution: Slackware 10.1
Posts: 142

Original Poster
Rep: Reputation: 15
I've already fixed my code....
I was just missing a *
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:31 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration