Thread: How to print number of characters per line

  1. #16
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by QuantumPete View Post
    %c prints a char, not a string. For strings you need to pass a char pointer and use %s
    To be fair, I did mention this above too: "You also want to print a STRING, not a char,"

    But good to see that the program's progressing nicely. When this thread started you didn't "have any idea", "cant figure it out", etc. and now you're 99% of the way to finishing the thing. And how much did we actually have to do for you? Virtually zero.

    Keep going.

    - 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.

  2. #17
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by ledow View Post
    To be fair, I did mention this above too: "You also want to print a STRING, not a char,"

    But good to see that the program's progressing nicely. When this thread started you didn't "have any idea", "cant figure it out", etc. and now you're 99% of the way to finishing the thing. And how much did we actually have to do for you? Virtually zero.

    Keep going.
    Yes, I too am amazed by the sudden burst of determination. As long as he didn't copy it from somewhere, which does not seem to be the case, kudos.
    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.

  3. #18
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    If I have a variable result,which is "number_of_char_per_line/number_of_array_per_line", and I wan to printf("%(result)s ",word),how can i do it? I cant just put the result in there,I will print out result,but I want the integer stored in the variable result.Will it be better for understanding if I show my code?

  4. #19
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by Slyvester Ping View Post
    If I have a variable result,which is "number_of_char_per_line/number_of_array_per_line", and I wan to printf("%(result)s ",word),how can i do it? I cant just put the result in there,I will print out result,but I want the integer stored in the variable result.
    Eh?

    Quote Originally Posted by Slyvester Ping View Post
    Will it be better for understanding if I show my code?
    Yes.

    - 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.

  5. #20
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Oh, hold on. You mean:

    You want to print a number in the middle of a printf statement? Yes?

    Assuming "number" is an integer:

    Code:
    printf("The next thing is your number %i and nothing else", number);
    There are other "%whatever's" that indicate unsigned integers, floats, strings, etc. so you can do something like this:

    Code:
    int number1 = 0;
    int number2 = 0;
    char *string1 = "Fred"
    
    printf("This is the first integer %i, this is the second %i, and this is a string %s", number1, number2, string1);

    - 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.

  6. #21
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    No, I was saying that,normally we use printf("%6s",word). But now,if I'm have a variable "int result",how can I replace the 6 in the printf with result? Because,there will be different arrays of words in a line,so i want to format them according to the arrays.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could read the man page.
    Code:
    Field Width:
    	     An optional digit string specifying a field width; if the output
    	     string has fewer characters than the field width it will be
    	     blank-padded on the left (or right, if the left-adjustment indi-
    	     cator has been given) to make up the field width (note that a
    	     leading zero is a flag, but an embedded zero is part of a field
    	     width);
    
    A field width or precision may be `*' instead of a digit string.  In this
         case an argument supplies the field width or precision.

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

  8. #23
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    HI slyvester,

    why dont you try functions getchar() and putchar().. your assignment is is easily achievable with this functions.. while you have to include ctype.h header... which eventually helps you to understand strings in better...Playing around with strings really need some programming experience ..

    Code mite look like this

    Code:
    #include <stdio.h>
    #include <ctype.h>
    char ch;
     while(( ch=getchar() )!= 'q')
         {
    implement New line for every 20th character
    
    print ch
    
    }

  9. #24
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by seemaxie View Post
    HI slyvester,

    why dont you try functions getchar() and putchar().. your assignment is is easily achievable with this functions.. while you have to include ctype.h header... which eventually helps you to understand strings in better...Playing around with strings really need some programming experience ..

    Code mite look like this

    Code:
    #include <stdio.h>
    #include <ctype.h>
    char ch;
     while(( ch=getchar() )!= 'q')
         {
    implement New line for every 20th character
    
    print ch
    
    }
    Donny..... please.
    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.

  10. #25
    Registered User
    Join Date
    Mar 2012
    Posts
    35
    Hi claudie .. i am sorry.. ctype.h is not necessary .. I am was bit curious to achieve this but could not able to implement his one of his rules... (Also the end of the line cant be half of the word)- could not able to implement ..... how to get control on words.. or it is better to achieve this with strings???

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        char ch;
        int i=0,word=1,j=1;
        printf("Press Num '1' to Quit\n");
        printf("Please Enter below 200 Chars\n");
        printf("Q) What is the difference between a mathematician and a computer scientist?\nA) ");
       while(( ch=getchar() )!= '1' && j<=200 )
    {
        if(ch == ' ' || ch == '\t')
        word++;
        i++; j++;
        if(i != 20)
        {
            putchar(ch);
        }
        else if(i==20)
        {
            putchar(ch);
            putchar('\n');
            i=0;
        }
    }
    
    
    printf(" \nnumber of words %d",word);
       return 0;
    }

  11. #26
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Generally I think the easiest way to do it is using the information quzah provided.

    As for your code, one obvious problem you are having, and is generally a problem with getchar() is that when you input a character you automatically feed the input buffer 2 characters(the one you typed and the return key), so you need another getchar() immediately after to discard the second.

    To be honest, there have been very few times, if any, when I thought to myself "getchar" may be of great help here, so I generally steer clear of 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.

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, 01:36 PM
  4. Replies: 0
    Last Post: 03-28-2003, 08:20 AM
  5. Graphics won't print characters
    By cheesehead in forum C++ Programming
    Replies: 0
    Last Post: 11-12-2001, 11:45 AM