# Thread: How to print number of characters per line

1. ## How to print number of characters per line

I dont have any idea about my assignment,a pseudo code or even better,part of the code,will help me a lot~
Well,my program should ask the user to enter a paragraph of words,then output the words according to 20 char per line.Also the end of the line cant be half of the word.I cant explain it quite well,but the example below shows what i should do.

Enter some sentences (between 150 ¨C 200 chars): What¡¯s the
difference between a mathematician and a computer scientist?
computer scientist has all the answers and no theories.

Formatted text:
What's the difference between
a mathematician and a computer
mathematician has all the
computer scientist has all the

2. oops,sorry~i left out one part,all last letter cant be spaces,so it means that the spaces left should be devided amongly between the line.

3. Originally Posted by Slyvester Ping
pseudo code or even better,part of the code,will help me a lot
No, that's not how it works here. You make an effort and show us what you've tried and we'll help you fix bugs and suggest better ways of doing things.

4. There is a reason this is your assignment, and the reason is YOU have to do it. We can help along the way, but we will not start it for your, nor will we spoon feed you all of it. A decent amount of effort has to come from you.

5. But I cant figure it out,and i just wan some pseudo code or the algorithm how to do it so that I know where to start.

6. Start with a pencil and paper. Figure out how you would solve this problem. Then describe the steps you have taken. Then generalise the steps you have taken. Then you are ready to describe this process in a programming language so even a stupid computer can do it.

7. Show us what you've tried so far.

8. first,I read in the paragraph,then use a string tokenizer to tokenize every words with a blank space.
second,I make every token an array of char.(But I get weird symbol while doing this,and not sure if i can do it so.)
third,i loop through every array and count the number of char in it(make sure it doesn't exceed the number).
then count the array that i have,and printf with %(number_per_line/array_count) .

10. Code:
```#include <stdlib.h>
#include <string.h>

#define SIZE 200
#define number 150
#define perline 5

int main(void)
{
char para[SIZE]="Where there is will there is a way.";
char *ch;
int i=0;
char *word[10];
ch=strtok(para," ");
while(ch!=NULL)
{
word[i]=&ch;
ch=strtok(NULL," ");
printf("%c",*word[i]);
}
return 0;
}```

11. Good-o. You don't want to take the ADDRESS of ch on this line:

Code:
` word[i]=&ch;`
so remove the &. It's already a pointer to a char, and so is word[i].

You also want to print a STRING, not a char, in your printf, and again you don't need to deference that (no need for *) because it's already a pointer to a char (which is also called a string).

Oh, and you need to include stdio.h if you want to use printf, I think. All of those should have been caught by compiler warnings at some point, so TURN YOUR COMPILER WARNINGS ON.

That's the first two parts of your thinking done (though I wouldn't use the pointers after strtok has been through mangling the original string, personally, but I don't think there's anything too wrong with that).

Now you need to loop through the array of words you have, count the number of characters in each, and work out when you "go over" 20 characters. When you do, what do you intend to do?

See how, with almost zero "help", you've written the program yourself? That's what we knew would happen - it's not just us being lazy.

12. well,almost done part3,but cant printf the words and program crash.
Code:
```#include <stdlib.h>
#include <string.h>

#define SIZE 200
#define number 150
#define perline 5

int main(void)
{
char para[SIZE]="Where there is will there is a way.";
char *ch;
int i;
int x=0;
char *word[10];
ch=strtok(para," ");
while(ch!=NULL)
{
word[i]=ch;
ch=strtok(NULL," ");
printf("%c\n",word[i]);
}
for(i=0;i<10;i++)
{
if(x+strlen(word[i])<=perline)
{
x+=strlen(word[i]);
printf("%c",*word[i]);
}
else
{
printf("\n");
x=0;
printf("%c",*word[i]);
}
}

return 0;

}```

13. %c prints a char, not a string. For strings you need to pass a char pointer and use %s

14. ok,now i just need to optimised my code,so that blank spaces wont appear in the last place.For this part of code:
Code:
`for(i=0;i<10;i++)`
is there a way to know how many token are being created or if word[i] exist,rather than hardcoding i<10?

15. Sure, make a counter that you increment every time you get a successful strtok.

Quzah.