Sorting strings from the text file
Given a text file “file.txt” that consists of strings, the task is to sort all the strings in alphabetical order in that text file.
Approach: The idea is to use the concept of File Handling and a text file(say file.txt) that contains all the strings. Below are the steps:
- Create the file using fopen() and insert names into the file using fprintf().
- Close the file using fclose().
- Reopen the file for reading the names.
- Read or scan the names from the file using fscanf() and store it in a vector of strings.
- Sort the given string stored in the vector using the sort() function.
- Now, insert the sorted string in that file and print it.
Below is the implementation of the above approach:
C++
// C++ program to sort given array // of string stored in a file #include <bits/stdc++.h> #include <cstdlib> #include <cstring> #include <fstream> using namespace std; // Driver Code int main() { int N, i, j; // File pointer to open file FILE * f; // fopen() for creating of a file f = fopen ( "file.txt" , "w" ); // Input number of strings // to be inserted in file cin >> n; vector< int > name(N); // Insert the strings into file for (i = 0; i < n; i++) { // Insert names in file cin >> name[i]; // Writing into the file fprintf (f, "%s" , name[i]); } // Close the file fclose (f); // Reopening in read mode f = fopen ( "file.txt" , "r" ); // Check does file exist or not if (f == NULL) { cout << "File doesn't exist!" ; return 0; } // Read the file until it // encounters end of line while (! feof (f)) { fscanf (f, "%s" , name[i]); i++; } n = i - 1; // Sort the strings sort(name.begin(), name.end()); // Insert the strings into file // after sorting for (i = 0; i < n; i++) { // Write into the file fprintf (f, "%s" , name[i]); } // Print the sorted names for (i = 0; i < n; i++) { cout << name[i] << '\n' ; } return 0; } |
Python3
# Python program to sort given array of string stored in a file # Input number of strings to be inserted in file N = int ( input ( "Enter the number of strings: " )) # Open file for writing with open ( "file.txt" , "w" ) as f: # Insert the strings into file for i in range (N): name = input ( "Enter the string: " ) # Writing into the file f.write(name + "\n" ) # Open file for reading with open ( "file.txt" , "r" ) as f: # Read the lines until end of file is reached names = [line.strip() for line in f.readlines()] # Sort the strings names.sort() # Open the file for writing with open ( "file.txt" , "w" ) as f: # Insert the sorted strings into the file for name in names: f.write(name + "\n" ) # Print the sorted names for name in names: print (name) |
Java
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class SortStringsFromFile { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input number of strings to be inserted in file System.out.print( "Enter the number of strings: " ); int n = scanner.nextInt(); scanner.nextLine(); try { // Open file for writing BufferedWriter writer = new BufferedWriter( new FileWriter( "file.txt" )); // Insert the strings into file for ( int i = 0 ; i < n; i++) { System.out.print( "Enter the string: " ); String name = scanner.nextLine(); // Writing into the file writer.write(name + "\n" ); } // Close the writer writer.close(); // Open file for reading BufferedReader reader = new BufferedReader( new FileReader( "file.txt" )); // Read the lines until end of file is reached List<String> names = new ArrayList<String>(); String line; while ((line = reader.readLine()) != null ) { names.add(line); } // Close the reader reader.close(); // Sort the strings Collections.sort(names); // Open the file for writing writer = new BufferedWriter( new FileWriter( "file.txt" )); // Insert the sorted strings into the file for (String name : names) { writer.write(name + "\n" ); } // Close the writer writer.close(); // Print the sorted names for (String name : names) { System.out.println(name); } } catch (IOException e) { e.printStackTrace(); } scanner.close(); } } |
Input File:
Output File:
Please Login to comment...