Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 9:12 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: sort words in c++
PostPosted: Sat Mar 26, 2011 10:16 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2272
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

following code read words from text file and sort it.
Code:
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <iomanip>
using namespace std;
// words file path.
#define in_file "data.txt"
// max number of words.
const int maxNumWords 10;
// function prototype
void readStrings(char[], char[], char[], char[]);
void sortStrings (char [], char [], char [], char []);
void swapStrings(char [], char []);
void printStrings(char [], char [], char [], char []);
ifstream fileStream// global variable 

int main()
{
 
    
char string1[maxNumWords], string2[maxNumWords], string3[maxNumWords], string4[maxNumWords];
    
char next_char;
    
fileStream.open("data.txt");
    
// Read the data.txt file until the end.
    
while(!fileStream.eof())
    {
        
readStrings(string1string2string3string4);
        
sortStrings(string1string2string3string4);
        
// print the words group after sorting.
        
printStrings(string1string2string3string4);
        
cout << endl;
        
fileStream.get(next_char);
    }
    
cout << endl;
    
// close the file stream.
    
getch();
    
fileStream.close();
    return 
0;
    
}
void readStrings(char string1[], char string2[], char string3[], char string4[])
{
fileStream >> string1 >> string2 >> string3 >> string4;
}
void sortStrings(char str1[], char str2[], char str3[], char str4[])


    
// putting largest in its place
if (strcmp(str1str2) > 0)
    
swapStrings(str1str2);
if (
strcmp(str2str3) > 0
    
swapStrings(str2str3);
if (
strcmp(str3str4) > 0
    
swapStrings(str3str4);

    
// putting second largest in its place
if (strcmp(str1str2) > 0
    
swapStrings(str1str2); 
if (
strcmp(str2str3) > 0
    
swapStrings(str2str3);

    
//putting third largest in its place
if (strcmp(str1str2) > 0
    
swapStrings(str1str2);
}


void swapStrings(char string1[],char string2[])
{
    
char temp[maxNumWords];
    
strcpy(temp,string1);
    
strcpy(string1,string2);
    
strcpy(string2,temp);
}


void printStrings(char string1[], char string2[], char string3[], char string4[])
{
    
cout << left << setw(9) << string1 << left << setw(9);
    
cout << string2 << left << setw(9);
    
cout << string3 << left << setw(9);
    
cout << string4 << left << setw(9);


}
 

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 Change the letter spacing of the words
 Library Sort
 Breaking the String into Words
 get number of words in a string
 Help with selection sort for strings?
 probability of repeated words
 Array sort
 Sort a list
 String Sort
 Quick Sort

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team