Skip to content

Instantly share code, notes, and snippets.

@tmplt
Created June 7, 2015 20:09
Show Gist options
  • Save tmplt/49974cb3a8aa292ad97b to your computer and use it in GitHub Desktop.
Save tmplt/49974cb3a8aa292ad97b to your computer and use it in GitHub Desktop.
Enter a word, and get it in diamond-form
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
if (argc < 2) return 1;
string word = argv[1];
unsigned int out = word.length() / 2;
int in = 1;
unsigned int ch = 0;
for (; out > 0; out--, ch++) {
cout << string(out, ' ') << word[ch];
if (ch > 0) {
cout << string(in, ' ') << word[ch];
in += 2;
}
cout << endl;
}
cout << word[ch] << string(in, ' ') << word[ch] << endl;
ch++; out++; in -= 2;
for (; out < word.length() / 2; out++, ch++)
{
cout << string(out, ' ') << word[ch];
if (ch < word.length())
{
cout << string(in, ' ') << word[ch];
in -=2;
}
cout << endl;
}
cout << string(out, ' ') << word[ch] << endl;
}
@tmplt
Copy link
Author

tmplt commented Jun 7, 2015

$ diword computers
    c
   o o
  m   m
 p     p
u       u
 t     t
  e   e
   r r
    s

Doesn't work with even word-lengths, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment