Skip to content

Instantly share code, notes, and snippets.

@junfenglx
Created March 2, 2015 17:41
Show Gist options
  • Save junfenglx/e7840f9aa9faf022f56e to your computer and use it in GitHub Desktop.
Save junfenglx/e7840f9aa9faf022f56e to your computer and use it in GitHub Desktop.

Revisions

  1. junfeng_hu created this gist Mar 2, 2015.
    44 changes: 44 additions & 0 deletions word_triangles.cc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    #include <iostream>
    #include <vector>
    #include <string>

    void print(const std::string &s, size_t i) {
    size_t len = s.length();
    size_t space_count = i < len ? i : len;
    for(size_t j = 0; j < space_count; ++j)
    std::cout << " ";
    if (space_count < len)
    std::cout << s.c_str() + i;
    }
    int main()
    {
    using namespace std;
    vector<string> words;

    size_t max_len = 0;

    while(true)
    {
    string s;
    cin >> s;
    if (!cin)
    break;
    string t(s);
    if (max_len < t.length())
    max_len = t.length();
    words.push_back(t);
    }

    for (size_t i=0; i < max_len; ++i)
    {
    vector<string>::const_iterator start = words.begin();
    print(*start, i);
    for(start = start + 1; start != words.end(); ++start)
    {
    cout << " ";
    print(*start, i);
    }
    cout << endl;
    }
    return 0;
    }