| | |
alphabet sorting for strings
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 28
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<string.h> int n; char (*p)[40],*temp; void Input(); void ABCsorting(); void Input() { printf("How many persons u want to input: "); scanf("%d",&n); p=(char*)malloc(n*40*sizeof(char)); temp=(char*)malloc(n*sizeof(char)); int i; for(i=0; i<n ; i++) { printf("String %d: ",i+1); fflush(stdin); gets(p[i]); } printf("All strings before sorting: \n"); /*for(i=0; i<n ; i++) { printf(" %s\n",*(p+i) ); } */ for(i=0; i<n ; i++) printf(" %s |",p[i]); } void ABCsorting() { int i,j; for(i=0; i<n-1 ;i++) for(j=i+1; j<n; j++) { if( strcmp((p+i),(p+j)) > 0 ) { temp = (p+i); (p+i) = (p+j); (p+j) = temp; } /* if( strcmp(p[i], p[j]) > 0) { temp=p[i]; p[i]= p[j]; p[j]=temp; }*/ } printf("\nAfter Sorting: \n"); for(i=0; i<n ;i++) printf(" %s |",p[i]); } int main() { Input(); ABCsorting(); getch(); return 0; }
Thanks a lot!
-7
#2 21 Days Ago
>> line 20: fflush(stdin);
fflush() is only guaranteed to work with output streams, not input streams.
>> line 21: gets(p[i])
Two problems:
>>line 38: if( strcmp((p+i),(p+j)) > 0 )
Delete that and uncomment the algorithm on line 44.
fflush() is only guaranteed to work with output streams, not input streams.
>> line 21: gets(p[i])
Two problems:
- never ever use gets() because it may write beyone the bounds of the array, causing your program to crash. Use fgets() instead.
- Variable p is an array of 40 pointers. You have to allocate memory for those pointers before they can be used. Suggest you code it something like this:
char p[40][80];, which is an array of 40 character arrays, and each array can hold up to 80 characters.
>>line 38: if( strcmp((p+i),(p+j)) > 0 )
Delete that and uncomment the algorithm on line 44.
Last edited by Ancient Dragon; 21 Days Ago at 10:28 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Help with sorting strings alphabetically (Java)
- Sorting 3 strings in c++ using if else-if (C++)
- Sorting strings - segmentation fault (C)
- sorting strings into alphabetical order (Java)
- Need help, searching & sorting (C)
- radix with strings of integers (C++)
- Rearranging and Sorting C strings. (C++)
Other Threads in the C Forum
- Previous Thread: classes
- Next Thread: huffman code
| Thread Tools | Search this Thread |
* adobe api array arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o ide inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






