0

I am trying to write a program that takes user-input strings and prints them out as a 7 by 5 asterisk grid. I am currently trying to figure out how to print put the letters as I need to print them line by line in order for the different letters to be printed side by side.

Anyway, my attempt so far is this:

#include <stdio.h>
#include <string.h>


char capitalA[7][5] = { 
"  *  ",
" * * ",
"*   *",
"*****",
"*   *",
"*   *",
"*   *"
};
int i;
int j;

int main() 
{//main

    for (i = 0; i < 5 ; i++) 
    {
        for (j = 0; j < 7 ; j++) 
        {
            printf("%s\n", capitalA[j][i]);
        }
    }
    return (0);
}//*main

My desired output is the asterisk A, but I get a Segmentation fault.

1
  • %s --> %c (voting to close as a typo). Commented Oct 24, 2015 at 11:42

5 Answers 5

4
char capitalA[7][5]

is not large enough. The strings literals you use in the initialization automatically include the \0 character, so you need to use

char capitalA[7][6] 

instead. And change your loops to a single one

for (j = 0; j < 7 ; j++)
    printf("%s\n", capitalA[j]);

since right now you are displaying chars using %s specifier.

If you're not happy with the '\0' being added automatically and need to save space, you can use the code you currently have or you need to initialize the array using brace initialization like

char capitalA[7][5] = {{' ', ' ', '*', ' ', ' '}, ...};

and stick to displaying the characters char by char (making sure that you use the '%c' specifier instead of the '%s').

Sign up to request clarification or add additional context in comments.

8 Comments

@MiguelRestrepo It should work fine, see the live example here.
Well then I don't know what is wrong with my compiler, but thank you anyway.
@MiguelRestrepo Did you do exactly what vsoftco told to? Could you share your new code which segfaults?
@MiguelRestrepo Can you copy paste the code in the live example I just linked and try it? There is no reason to get a segfault.
Wow, that was stupid. I'm sorry, I forgot to remove the [i] from the printf statement after deleting the i for loop.
|
2

This:

for (i = 0; i < 5 ; i++) {
    for (j = 0; j < 7 ; j++) {
        printf("%s\n", capitalA[j][i]);
}
}

should be

for (i = 0; i < 7; i++) {
    for (j = 0; j < 5; j++) {
        printf("%c", capitalA[i][j]);
    }
    printf("\n");
}

because

  1. The correct format specifier for a char is %c, not %s.
  2. You mixed up the loops. The outer one should loop 7 times while the inner one should loop 5 times.
  3. You should print a \n after the inner loop has finished executing and not in each iteration of the inner loop.

Comments

1
printf("%s\n", capitalA[j][i]);

should be

printf("%c", capitalA[j][i]);

What you need is to print character by character but you are trying to print a string using %s which tries to print a string until \0 is encountered.

Since %s is trying to access array out of bound you see a segmenation fault.

If you want to go with printing char by char then make sure you insert \n after each row

Comments

0
 for (i = 0; i < 5 ; i++) 
 {
    for (j = 0; j < 7 ; j++) 
    {
        printf("%s\n", capitalA[j][i]);
    }
 }

You are printing char type array. Not a single character. Change it to

for (i = 0; i < 5 ; i++) 
{
    for (j = 0; j < 7 ; j++) 
    {
        printf("%c", capitalA[j][i]);
    }
    printf("\n");
}

1 Comment

This prints each character and then start a newline. Which means I get blank lines and lines with single asterisks.
0

just use one for loop, and change 5 to 6 in the array declaration like this :

char capitalA[7][6]
//  and
for(j=0 ; j<7 ; j++)
    printf("%s\n",capitalA[j]);

your program will be like this after modification, try it ^^:

#include <stdio.h>
#include <string.h>

//edit the column nomber to support the '\0' it will be 6 not 5.
char capitalA[7][6] = { 
"  *  ",
" * * ",
"*   *",
"*****",
"*   *",
"*   *",
"*   *"
};
int i;
int j;

int main() 
{//main


        for (j = 0; j < 7 ; j++) 
        {
            printf("%s\n", capitalA[j]);
        }

    return (0);
}//*main

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.