Parallelogram Words


TCEA State Programming Contest

Problem 9.6 Parallelogram Words

General Statement:
Output a given word horizontally and multiple times vertically so that each letter in the horizontal word matches the position of that letter vertically.

Input:
The first line in the data set is an integer that represents the number of words that follow. Each word is on a separate line.

Output:
The horizontal word is to be in the center of the output. Each vertical word is to read down from the top. The first vertical word uses the first letter of the horizontal word. The last vertical word uses the last letter of the horizontal word.

All outputs are to have the same left edge vertically.

The output is to be formatted exactly like that for the sample output given below.

Assumptions:
All letters are upper case. The maximum word length is 10.

Sample Input:
3
TEST
SAMPLE
ART

Sample output:


   T
  TE
 TES
TEST
EST
ST
T

     S
    SA
   SAM
  SAMP
 SAMPL
SAMPLE
AMPLE
MPLE
PLE
LE
E

  A
 AR
ART
RT
T

Solution:


#include 
using namespace std;

int main(int argc, char **argv)
{
	char a [12];

	int test_case;
	cin >> test_case;

	while ( test_case-- ) {

		cin >> a;

		int i, j, k;
		int string_length = strlen (a);

		for ( i = 0; i < string_length; i++ ) { 			for ( j = string_length - 2 - i; j >= 0; j-- )
				cout << ' ';
			for ( k = 0; k <= i; k++ )
				cout << a [k];
			cout << '\n';
		}

		for ( i = 0; i < string_length; i++ ) {
			for ( j = i + 1; j < string_length; j++ )
				cout << a [j];
			cout << '\n';
		}
	}

	return 0;
}

Critical Case:

Critical Input:
2
c
bp

Critical Output:
c

 b
bp
p

5 thoughts on “Parallelogram Words

  1. copied and tried to test the code, and it gives me errors
    like the strlen can’t be used as a function
    and invalid operand of types int and char to binary operator

  2. this following program works well:

    #include
    #include
    int main(){
    char word[12];
    int i,j,k,l,x,m,n,p,r;
    char s;
    printf(“please enter the number of inputs: “);
    scanf(“%d”,&x);
    fgets(word,12,stdin);
    for(i=1;i<=x;i++){
    printf("please enter a word of 10 characters or less: ");
    fgets(word,12,stdin);
    for(k=0;k<12;k++){if(word[k]=='\n'){word[k]='\0';}}
    l=strlen(word);
    s=' ';
    for(m=2;m=m){
    for(j=1;j0;n–){
    printf(“%c”,word[l-n]);}
    printf(“\n”);}
    }
    printf(“%s\n”,word);
    for(p=10;p>0;p–){
    if(l>=p){
    for(r=p-1;r>0;r–){
    printf(“%c”,word[l-r]);}
    printf(“\n”);
    }
    }
    }
    return 0;
    }

  3. My version in Lua

    function shift(self) return self:sub(2,self:len())..self:sub(1,1) end — rotate string self

    s=’TEST’ — string to build
    L = s:len()
    ss=string.rep(‘ ‘, L)
    S = ss..s..ss

    for i=1,2*L-1 do
    S = shift(S)
    print(S:sub(1,L))
    end

  4. or just like that inside four lines

    Word = ‘TESTWORD’
    L = string.len(Word)
    TransformedWord = string.rep(‘ ‘, L)..Word..string.rep(‘ ‘, L)
    for i=2,2*L do print(TransformedWord:sub(i,i+L-1)) end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s