Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,562 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,887 people online right now. Registration is fast and FREE... Join Now!




Sorting Strings

 
Reply to this topicStart new topic

Sorting Strings, difficult string sort

blurr_001
11 Apr, 2008 - 12:29 PM
Post #1

New D.I.C Head
*

Joined: 14 Mar, 2008
Posts: 9

ALPHABETIZING INPUTS FROM FILE input.... im stuck and getting frustrated.. tried everthing i can think of


this is what i need to sort
XMNP154
ALDS623
KMNT438
PRHU967
HGSE287
LKNM435
DETR178
OPRT348
LRHY412
JKLP693
PLKM563
TSRL923
MNOR789
JKLN610
EMKM579
TSCL623
MNKQ712

both alphabetically and unsorted.. SOMEONE PLEASE HELP


CODE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSTR 17

void funcinput(int numstr, char *strptr1[ ]);
void funcsort(int numstr, char *strptr1[ ]);
void funcoutput(int numstr, char *strptr[ ], char[ ]);
FILE *inptr, *outptr;
    
int main()
{

    char *strptr[MAXSTR];
    int numstr;
    
    scanf("%c", &numstr );
    if (numstr > MAXSTR)
        numstr = MAXSTR;    

    funcinput(numstr, strptr);
    funcoutput(numstr,strptr, "UNSORTED STRING");
    funcsort(numstr, strptr);
    funcoutput(numstr, strptr, "SORTED STRING");
    
    
    
    return 0;
}

/******************************************************************************/
/*                                                                            */
/*         funinput:  input character strings into dynamic string and store   */
/*                    the addresses in a character pointer array              */
/******************************************************************************/
/*                                                                            */
/*   Output Parameters:                                                       */
/*     *strptr1[NUMSTR]           character pointer array                     */
/*                                                                            */
/*   Input Variables:                                                         */
/*     strname1[STRLNG]           character string buffer                     */
/*                                                                            */
/******************************************************************************/
void funcinput(int numstr, char *strptr1[ ])
{
     int const STRLNG = 80;
     int i;
     char strname[STRLNG];
    
     FILE *inptr;
    
     inptr = fopen("sortin.dat", "r");
     fscanf(inptr, "%c",&i);
     for(i = 0; i < numstr; i++)
     {
           fgets(strname , STRLNG, inptr);
           strptr1[i] = (char *)malloc(strlen(strname));
           strcpy(strptr1[i], strname);
           }
           fclose(inptr);
          
           return;
           }
          
/******************************************************************************/
/*                                                                            */
/*         funcsort:                sort the character strings                */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/*   Input Parameters:                                                        */
/*     strptr1[ ]                  unsorted character string array pointers   */
/*                                                                            */
/*   Computed Variables:                                                      */
/*     none                                                                   */
/*                                                                            */
/*   Retruned Parameters:                                                     */
/*     strptr1[ ]                 sorted character string array pointers      */
/*                                                                            */
/******************************************************************************/
void funcsort(int numstr, char *strptr1[ ])
{
     int i, j;
     char *strtemp;
    
     for(i = 0; i < numstr - 1; i++)
     {
           for (j = i + 1; j < numstr; j++)
           {
               if(strcmp(strptr1[i], strptr1[j]) > 0)
               {
                   strtemp = strptr1[i];
                   strptr1[i] = strptr1[j];
                   strptr1[j] = strtemp;
                   }
                   }
                   }
     return;
}

/******************************************************************************/
/*                                                                            */
/*       funcoutput:  Output the character strings                            */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/*   Input Parameters:                                                        */
/*     strptr1[ ]                   character string arrays                   */
/*                                                                            */
/*                                                                            */
/*                                                                            */
/*                                                                            */
/*                                                                            */
/*                                                                            */
/*                                                                            */
/*                                                                            */
/*                                                                            */
/******************************************************************************/
void funcoutput(int numstr, char *strptr1[ ], char title[ ])
{
     FILE *outptr;
     int i;
    
     outptr = fopen("sortout.dat", "w");
    
     fputs(title, outptr);
    
     for(i = 0; i < numstr; i++)
     {
           fputs(strptr1[i], outptr);
           fprintf(outptr,"%c");
           }
           return;
           }


User is offlineProfile CardPM
+Quote Post

skaoth
RE: Sorting Strings
11 Apr, 2008 - 01:20 PM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 343



Thanked: 10 times
Dream Kudos: 100
My Contributions
This should work for you. I only slightly modified your code.
I did nothing with the sort function
CODE

int funcinput(char *strptr1[ ])
{
    int const STRLNG = 80;
    int i = 0;
    char strname[STRLNG];

    FILE *inptr;

    inptr = fopen("sortin.dat", "r");

    //fscanf(inptr, "%c",&i);
    while(!feof(inptr))
    {
        if(fgets(strname , STRLNG, inptr) != NULL)
        {
            strptr1[i] = (char *)malloc(strlen(strname));
            strcpy(strptr1[i], strname);
            i++;
        }
        
    }
    fclose(inptr);

    return i;
}

void funcoutput(int numstr, char *strptr1[ ], char title[ ])
{
    FILE *outptr;
    int i;
    

    outptr = fopen("sortout.dat", "a");

    fputs(title, outptr);

    for(i = 0; i < numstr; i++)
    {
        fputs(strptr1[i], outptr);
        //fprintf(outptr,"%c");
    }

    fclose(outptr);
    return;
}

int _tmain(int argc, _TCHAR* argv[])
{
    char *strptr[MAXSTR];
    int numstr = 0;


    // Truncate output file
    FILE *outptr = fopen("sortout.dat", "w");
    fclose(outptr);

    numstr = funcinput(strptr);
    funcoutput(numstr,strptr, "UNSORTED STRING\n");
    funcsort(numstr, strptr);
    funcoutput(numstr, strptr, "SORTED STRING\n");


    system("pause");
    return 0;
}

good luck

User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:44PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month