repl.it
@SPQR/

Alphabetical Orderer

C++11

^

fork
loading
Files
  • main.cpp
  • input.txt
  • output.txt
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>

bool isFirst(std::string str1, std::string str2) {
	int higherSize;
	if (str1.size() > str2.size()) higherSize = str1.size();
	else higherSize = str2.size();
	for (int i = 0; i < higherSize; i++) {
		char c1;
		char c2;

		if ((int)c1 >= 97) c1 -= 32;
		if ((int)c2 >= 97) c2 -= 32;
		
		//if both are the same until str1 ende, return true, else return false
		try {c1 = str1.at(i);}
		catch(std::out_of_range){return true;}
		try {c2 = str2.at(i);}
		catch(std::out_of_range){return false;}

		//compare value of character
		if ((int)c1 < (int)c2) return true;
		if ((int)c2 < (int)c1) return false;
	}
	return false;
}

int main() {

	std::ifstream input("input.txt");
	std::ofstream output("output.txt");

  std::cout << "Program started...\n";

	std::vector<std::string> strings;

	std::string line;
	std::getline(input, line); //to discard the first line
	while (std::getline(input, line)) {
		strings.push_back(line);
	}

	std::sort(strings.begin(), strings.end(), isFirst);

	for (int i = 0; i < strings.size(); i++) {
		output << strings.at(i) << "\n";
	}

	input.close();
	output.close();
}
?