0

I am trying to print out a two dimensional array of strings using the array shown below and associate each phrase with a number from 0 to 3. When I try to print out each phrase the words get matched together and print out incorrectly.

char PhraseList[4][10]= {" Work Hard","Play Hard ","Enjoy","Live"};

How can I print out each phrase on a separate line so that " Work Hard" prints out on one line then "Play Hard " on another line and then "Enjoy" on another etc. Also how can I associate each phrase with a number? Any help\suggestions will be greatly appreciated!

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>



int main()
{
char PhraseList[4][10]= {" Work Hard","Play Hard ","Enjoy","Live"};

int i;

for(i=0; i<4; i++)
{
    printf("%s \n", PhraseList[i]);
}


printf("\n\n");
system("PAUSE");
return 0;
}

Output:

 Work HardPlay Hard Enjoy
Play Hard Enjoy
Enjoy
Live


Press any key to continue . . .

2
  • 1
    Why are you having leading and trailing space in the first and second string entry? Is it intentional? If it is, you need to increase the size of the second dimension of the array. Commented Sep 24, 2015 at 6:54
  • 1
    Off-topic: if you really want to use system("pause") then why not use system("echo Work Hard") (etc.) as well? Commented Sep 24, 2015 at 8:15

5 Answers 5

3

Your printf calls are fine. The real problem is that you are overflowing the buffer. Each string has been given 10 bytes maximum storage. But in C strings are by definition NUL terminated. So you need an extra byte to store that NUL.

Better not to specify a fixed size at all. Can just do this:

const char *PhraseList[]= {" Work Hard","Play Hard ","Enjoy","Live"};
Sign up to request clarification or add additional context in comments.

Comments

3

As pointed out in the comment, the leading and trailing space in " Work Hard" and "Play Hard " respectively is the reason for issue.

The size of each of these is 11 characters (not 10).

" '[space]' 'W' 'o' 'r' 'k' '[space]' 'h' 'a' 'r' 'd' '\0'"

which results in 11 characters.

Hence increase the size of PhraseList and declare it as

char PhraseList[4][11]= {" Work Hard","Play Hard ","Enjoy","Live"};

or

const char *PhraseList[]= {" Work Hard","Play Hard ","Enjoy","Live"};

Comments

0

You allocate too little memory for your strings.

Lets look " Work Hard". In memory this string is stored as ' ', 'W', 'o', 'r', 'k', ' ', 'H', 'a', 'r', 'd', '\0', that is in it requires 11 bytes.

But you allocate 10 bytes only. Change type to PhraseList[4][11].

Comments

0

you could either get rid of your leading and trailing whitspaces or make you array bigger. If you want to output Number associated with the array just add the i variable to your output.

int main()
{
char PhraseList[4][10] = { "Work Hard","Play Hard","Enjoy","Live" };

int i;

for (i = 0; i<4; i++)
{
    printf("%d %s \n", i, PhraseList[i]);
}

printf("\n\n");
system("PAUSE");
return 0;
}

Comments

0
  • First Solution:

  • Just remove space from your array's first and second string.Because it is exceded the araay string length.

  • Second Solution:

  • Just increase your array's string size,

  • Instead of this,

    char PhraseList[4][10] = { " Work Hard","Play Hard ","Enjoy","Live" };
    
  • Declare it like this,

    char PhraseList[4][15] = { " Work Hard","Play Hard ","Enjoy","Live" };
    

3 Comments

why 15? just because SO comment minimum length is 15 characters ? :P
i have suggested 15 according to this code.It is depended on ur requirements or lenght of string.
And how exactly this code requires 15? Won't 11 do?

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.