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: