Diamond Word


General Statement: Read a word and output that word in a diamond pattern such that the entire word is the center horizontal line of the diamond and each row out from this center drops a letter on each end of the word.

Input: The first line of the data set for this problem is an integer that represents the number of strings that follow. Each string is on a separate line.

Output: Output the diamond pattern using all upper case letters.
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.

Discussion: If there are an odd number of letters, the top and bottom of the diamond will be the middle letter. If there are an even number of letters, the top and bottom of the diamond will be the middle 2 letters.

Sample Input:
3
SAMPLE
PROGRAM
PROBLEMS

Sample Output:

  MP
 AMPL
SAMPLE
 AMPL
  MP

   G
  OGR
 ROGRA
PROGRAM
 ROGRA
  OGR
   G

   BL
  OBLE
 ROBLEM
PROBLEMS
 ROBLEM
  OBLE
   BL
#include <stdio.h>
#include <string.h>

int main ()
{
    int dataset;
    scanf ("%d", &dataset);
    getchar ();

    while ( dataset-- ) {
        char input [12];
        gets (input);

        char save [5] [12];
        int length = strlen (input);

        if ( length % 2 ) { // if odd
            int index = length / 2;
            int count = index;
            for ( int i = 0; i < length / 2; i++ ) {
                strcpy (save [i], input);
                for ( int j = 0; j < index; j++ )
                    save [i] [j] = ' ';
                count++;
                save [i] [count] = 0;
                index--;
            }

            // output
            for ( int i = 0; i < length / 2; i++ )
                printf ("%s\n", save [i]);
            printf ("%s\n", input);
            for ( int i = length / 2 - 1; i >= 0; i-- )
                printf ("%s\n", save [i]);
        }

        else { // if even
            int index = length / 2;
            int count = index;
            for ( int i = 0; i < length / 2; i++ ) {
                strcpy (save [i], input);
                for ( int j = 0; j < index - 1; j++ )
                    save [i] [j] = ' ';
                count++;
                save [i] [count] = 0;
                index--;
            }

            // output
            for ( int i = 0; i < length / 2; i++ )
                printf ("%s\n", save [i]);
            for ( int i = length / 2 - 2; i >= 0; i-- )
                printf ("%s\n", save [i]);
        }
    }

    return 0;
}

One thought on “Diamond Word

  1. #include
    #include
    int main(){
    char word[12];
    int x,a,b,c,l,z;
    char d;
    printf(“please enter the number of inputs: “);
    scanf(“%d”,&x);
    fgets(word,12,stdin);

    for(z=1;z<=x;z++){
    printf("please enter at least a three-letter word: ");
    fgets(word,12,stdin);
    for(a=0;a<12;a++){if(word[a]=='\n'){word[a]='\0';}}
    l=strlen(word);
    d=' ';
    printf("\n");
    if(l%2==0){
    for(c=1;c=4){printf(“%c%c\n”,word[l/2-1],word[l/2]);}
    for(c=1;c=6){printf(“%c%c%c%c\n”,word[l/2-2],word[l/2-1],word[l/2],word[l/2+1]);}
    for(c=1;c=8){printf(“%c%c%c%c%c%c\n”,word[l/2-3],word[l/2-2],word[l/2-1],word[l/2],word[l/2+1],word[l/2+2]);}
    for(c=1;c=10){printf(“%c%c%c%c%c%c%c%c\n”,word[l/2-4],word[l/2-3],word[l/2-2],word[l/2-1],word[l/2],word[l/2+1],word[l/2+2],word[l/2+3]);}
    printf(“%s\n”,word);
    for(c=1;c=10){printf(“%c%c%c%c%c%c%c%c\n”,word[l/2-4],word[l/2-3],word[l/2-2],word[l/2-1],word[l/2],word[l/2+1],word[l/2+2],word[l/2+3]);}
    for(c=1;c=8){printf(“%c%c%c%c%c%c\n”,word[l/2-3],word[l/2-2],word[l/2-1],word[l/2],word[l/2+1],word[l/2+2]);}
    for(c=1;c=6){printf(“%c%c%c%c\n”,word[l/2-2],word[l/2-1],word[l/2],word[l/2+1]);}
    for(c=1;c=4){printf(“%c%c\n”,word[l/2-1],word[l/2]);}
    }
    else{
    for(c=1;c=3){printf(“%c\n”,word[l/2]);}
    for(c=1;c=5){printf(“%c%c%c\n”,word[l/2-1],word[l/2],word[l/2+1]);}
    for(c=1;c=7){printf(“%c%c%c%c%c\n”,word[l/2-2],word[l/2-1],word[l/2],word[l/2+1],word[l/2+2]);}
    for(c=1;c=9){printf(“%c%c%c%c%c%c%c\n”,word[l/2-3],word[l/2-2],word[l/2-1],word[l/2],word[l/2+1],word[l/2+2],word[l/2+3]);}
    printf(“%s\n”,word);
    for(c=1;c=9){printf(“%c%c%c%c%c%c%c\n”,word[l/2-3],word[l/2-2],word[l/2-1],word[l/2],word[l/2+1],word[l/2+2],word[l/2+3]);}
    for(c=1;c=7){printf(“%c%c%c%c%c\n”,word[l/2-2],word[l/2-1],word[l/2],word[l/2+1],word[l/2+2]);}
    for(c=1;c=5){printf(“%c%c%c\n”,word[l/2-1],word[l/2],word[l/2+1]);}
    for(c=1;c=3){printf(“%c\n”,word[l/2]);}
    }
    }

    return 0;
    }

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