Total members 11890 |It is currently Fri Apr 19, 2024 5:42 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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);


}
 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : sort words in c++
 probability of repeated words     -  
 Breaking the String into Words     -  
 get number of words in a string     -  
 Return words count in a string     -  
 Change the letter spacing of the words     -  
 Array sort     -  
 C++ Selection Sort     -  
 selection sort     -  
 Library Sort     -  
 Insertion Sort (C++)     -  



Topic Tags

C++ Sorting
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com