| | |
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 20 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; 20 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 |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






