fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. const int MAX_SIZE = 20;
  6.  
  7. void sortWords(char words[][MAX_SIZE + 1], int lastWord) {
  8. for (int i = 0; i < lastWord; ++i) {
  9. for (int j = i + 1; j < lastWord; ++j) {
  10. if (strcmp(words[i] , words[j]) > 0) {
  11. char aux[MAX_SIZE + 1];
  12. strcpy(aux, words[j]);
  13. strcpy(words[j], words[i]);
  14. strcpy(words[i], aux);
  15. }
  16. }
  17. }
  18. }
  19.  
  20. void convertLineToWords(char words[][MAX_SIZE + 1], char line[], int &lastWord) {
  21. for (int i = 0, j = 0; line[i]; ++i) {
  22. if (line[i] == ' ') {
  23. ++lastWord;
  24. j = i + 1;
  25. continue;
  26. }
  27. words[lastWord][i - j] = line[i];
  28. }
  29. if (words[lastWord][0]) {
  30. ++lastWord;
  31. }
  32. }
  33.  
  34. int main() {
  35. char firstLine[MAX_SIZE + 1];
  36. cin.getline(firstLine, MAX_SIZE + 1);
  37. char firstWords[MAX_SIZE / 2][MAX_SIZE + 1] = {0}, secondWords[MAX_SIZE / 2][MAX_SIZE + 1];
  38. int firstLastWord = 0, secondLastWord = 0;
  39. convertLineToWords(firstWords, firstLine, firstLastWord);
  40. while (cin >> secondWords[secondLastWord]) {
  41. ++secondLastWord;
  42. }
  43. sortWords(firstWords, firstLastWord);
  44. sortWords(secondWords, secondLastWord);
  45. for (int i = 0; firstWords[0][0] && i < firstLastWord; ++i) {
  46. for (int j = 0; secondWords[0][0] && j < secondLastWord; ++j) {
  47. cout << firstWords[i] << ' ' << secondWords[j] << '\n';
  48. }
  49. }
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5280KB
stdin
florin florin Flaviu
daniel ana
stdout
Flaviu ana
Flaviu daniel
florin ana
florin daniel
florin ana
florin daniel