I am trying to print the initials of My First Name "S S" using * pattern in C. However the patterns are printed one after the other. Is there any way to print the alphabets using * pattern one at the side of the other with a little space in between. I do not want to allot a very big box for both the alphabets. I want to code for individual alphabets and put them side by side with some space in between. My Code and out our is given below:
int k,l,i,j;
for(k=1;k<=7;k ){
for(l=1;l<=4;l ){
if(k==1 && l<=4){
printf("*");
}
else if(k>=2 && k<=3 && l==1){
printf("*");
}
else if(k==4 && l<=4){
printf("*");
}
else if(k>=5 && k<=6 && l==4){
printf("*");
}
else if(k==7 && l<=4){
printf("*");
}
else{
printf(" ");
}
}
printf("\n");
}
for(i=1;i<=7;i ){
for(j=1;j<=4;j ){
if(i==1 && j<=4){
printf("*");
}
else if(i>=2 && i<=3 && j==1){
printf("*");
}
else if(i==4 && j<=4){
printf("*");
}
else if(i>=5 && i<=6 && j==4){
printf("*");
}
else if(i==7 && j<=4){
printf("*");
}
else{
printf(" ");
}
}
printf("\n");
}
return 0;
}
Output:
****
*
*
****
*
*
****
****
*
*
****
*
*
****
However, I want my expected output to be:
**** ****
* *
* *
**** ****
* *
* *
**** ****
CodePudding user response:
The most important thing when approaching problems like this is to decompose your code in smaller pieces, using functions and/or typedefs or structs to organize things. In your case, you can easily separate the "knowledge" of how to print stars and spaces for your letters from the main code (which knows which letters to print in which order) using functions. Refactoring you code that way, we get:
void S(int k, int l) {
if(k==1 && l<=4){
printf("*");
}
else if(k>=2 && k<=3 && l==1){
printf("*");
}
else if(k==4 && l<=4){
printf("*");
}
else if(k>=5 && k<=6 && l==4){
printf("*");
}
else if(k==7 && l<=4){
printf("*");
}
else{
printf(" ");
}
}
int main() {
int k,l,i,j;
for(k=1;k<=7;k ){
for(l=1;l<=4;l ){
S(k, l);
}
printf("\n");
}
for(i=1;i<=7;i ){
for(j=1;j<=4;j ){
S(i, j);
}
printf("\n");
}
return 0;
}
Now you can easily rewrite main to print the letters side by side
int main() {
int k,l,i,j;
for(k=1;k<=7;k ){
for(l=1;l<=4;l ){
S(k, l);
}
printf(" ");
for(j=1;j<=4;j ){
S(k, j);
}
printf("\n");
}
return 0;
}
You could also extend this by writing different letters in different patterns (though having a function for each letter is somewhat verbose -- better would be to store the pixels for all letters in a data structure and print it that way)
CodePudding user response:
Use pattern matrix for every character you want to print. It'll make printing easier. You run through each letter of the string to print for every row in the pattern.
#include <stdio.h>
#include <string.h>
#define ROWMAX 7
#define COLMAX 5
#define CSPACE 1
#define PATTERN '#'
char cCube [26][ROWMAX][COLMAX] = {
{ {0, 1, 1, 1, 1}, // letter S
{1, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 0, 0, 1, 1},
{1, 0, 0, 1, 1},
{1, 1, 1, 1, 0}
},
{ {0, 1, 1, 1, 0}, // letter O
{1, 1, 0, 1, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 1, 0, 1, 1},
{0, 1, 1, 1, 0}
},
{ {1, 1, 1, 1, 0}, // letter D
{1, 0, 0, 1, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 0, 1},
{1, 0, 0, 1, 1},
{1, 1, 1, 1, 0}
}, //so on for alphabets you want to use
};
int chIndex [256] = {0};
void print_pat_msg (const char* msg) {
int mlen = strlen (msg);
for (int ri = 0; ri < ROWMAX; ri) {
for (int mi = 0; mi < mlen; mi) {
int chi = chIndex[ (unsigned char) msg[mi]];
for (int ci = 0; ci < COLMAX; ci)
(cCube[chi][ri][ci]) ? putchar (PATTERN) : putchar (' ');
for (int spi = 0; spi < CSPACE;) putchar (' ');
}
putchar ('\n');
}
printf ("\n");
}
int main () {
// if patterns are indexed alphabetically below initialisation can be omitted
chIndex[ (unsigned char) 'S'] = 0;
chIndex[ (unsigned char) 'O'] = 1;
chIndex[ (unsigned char) 'D'] = 2;
char message [] = "ODDS";
print_pat_msg (message);
return 0;
}
- This way you can design/customise the pattern for each letter with ease at one place. Code & pattern can be reused for other messages & names.
- Printing here works somewhat like a dot-matrix printer, striping across putting out pattern-line of each letter in the string one at a time.
- You can also replace
0
/1
flags with pretty much any character to draw ASCII images. You just have to adjust the pattern array, which is easy to follow & change at will. - Come to think of it, this is how fonts work at pixel level, patterns.
CodePudding user response:
My quickie solution:
#include <stdio.h>
static const unsigned char S[] = {0xF0, 0x80, 0x80, 0xF0, 0x10, 0x10, 0xF0};
int main(void) {
for(int i=0; i<sizeof(S); i)
{
for(int b=7;b; --b)
{
printf("%c", "* "[!(S[i] & (1<<b))]);
}
for(int b=7;b; --b)
{
printf("%c", "* "[!(S[i] & (1<<b))]);
}
printf("\n");
}
return 0;
}
Output:
Success #stdin #stdout 0s 5548KB
**** ****
* *
* *
**** ****
* *
* *
**** ****