Like Tree1Likes

How to print number of characters per line

This is a discussion on How to print number of characters per line within the C Programming forums, part of the General Programming Boards category; I dont have any idea about my assignment,a pseudo code or even better,part of the code,will help me a lot~ ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    12

    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): Whats the
    difference between a mathematician and a computer scientist?
    Answer: A mathematician has all the theories and no answers. A
    computer scientist has all the answers and no theories.


    Formatted text:
    What's the difference between
    a mathematician and a computer
    scientist? Answer : A
    mathematician has all the
    theories and no answers. A
    computer scientist has all the
    answers and no theories.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    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. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Slyvester Ping View Post
    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.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    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. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Show us what you've tried so far.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    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) .

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Post your code
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    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. #11
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    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.
    Last edited by ledow; 03-28-2012 at 06:53 AM.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    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. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    %c prints a char, not a string. For strings you need to pass a char pointer and use %s
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    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. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, make a counter that you increment every time you get a successful strtok.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to open files and print line by line in shell
    By omega666 in forum Linux Programming
    Replies: 4
    Last Post: 04-15-2011, 04:54 PM
  2. how to print characters of own language?
    By kantze in forum C Programming
    Replies: 4
    Last Post: 10-17-2006, 12:22 PM
  3. print line by line from a file
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 02-18-2005, 12:36 PM
  4. Replies: 0
    Last Post: 03-28-2003, 07:20 AM
  5. Graphics won't print characters
    By cheesehead in forum C++ Programming
    Replies: 0
    Last Post: 11-12-2001, 10:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21