LinuxQuestions.org
Visit Jeremy's Blog.
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 10-27-2003, 05:41 AM   #1
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Rep: Reputation: 15
Question simple program


http://www.linuxquestions.org/questi...hreadid=108981

the above link has a program for insertion sort for sorting integers. IMPORTANT NOTE : please change the line

esize = malloc(sizeof(int));

to

esize = sizeof(int);

now i want a program to sort strings(not only single letters but also words). i used char data[100]; instead of int data[100]; and in printf and scanf statements i used ("%d", &data[i]);
but the result is coming as:

[prasanth@chanduram prasanth]$ cc issort2.c
[prasanth@chanduram prasanth]$ ./a.out

Enter the number of elements : 5

Enter the list of elements :
a
b
d
e
c

After insertion sort the list of elements :
abcde
bcde
cde
de
e


and for words i get :

[prasanth@chanduram prasanth]$ ./a.out

Enter the number of elements : 5

Enter the list of elements :
elf
bat
dog
ant
cat

After insertion sort the list of elements :
abcdeat
bcdeat
cdeat
deat
eat


please help me
 
Old 10-27-2003, 12:03 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
You need to change your data_compare() function to compare strings instead of char's.
Hint: use strncpy()
<edit>
strncpy() is the wrong function.
I meant to say strncmp()
</edit>

Last edited by Hko; 10-27-2003 at 05:49 PM.
 
Old 10-27-2003, 03:06 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
bprasanth_20, please stick to one thread, do not repost a brand new thread when you change 1 line of code... i have closed you previous thread. thanks.
 
Old 10-28-2003, 03:45 AM   #4
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
HKo ur suggsetion is nice but the output is the same man
i think the problem is within the lines printf("%s", &data[i]); and scanf("%s", &data[i]); . please try to work out the program in ur linux c and help me
 
Old 10-28-2003, 12:01 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Just change data[] to hold strings instead of char's too.
Quote:
please try to work out the program in ur linux c and help me
Have you tried to find out yourself yet?
 
Old 10-29-2003, 12:55 AM   #6
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
not only me. 3 other pple tried it out. they souldnt find out the problem. thats why i asked u to run the program in ur linux machine.they told me to use a 2 dimensional array for data to hold charecters. but that requires changes in the program issort.c. but no changes should be made in issort.c. changes can be made only in issort1.c. To refer them use the link:

http://www.linuxquestions.org/questi...hreadid=108981
 
  


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
simple program help! rohitkara Linux - General 1 05-24-2005 08:57 PM
simple c program liguorir Linux - Software 4 05-29-2004 06:22 AM
simple C program problem mined Programming 2 05-08-2004 05:42 AM
Compiling a simple C++ program kbeaver Programming 27 02-06-2004 08:02 PM
Help with the following simple C program ..... purpleburple Programming 5 09-13-2002 10:52 PM

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

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

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