Created
June 7, 2015 20:09
-
-
Save tmplt/49974cb3a8aa292ad97b to your computer and use it in GitHub Desktop.
Enter a word, and get it in diamond-form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't work with even word-lengths, though.