Thread: HELP > Printing String Gradually!

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    HELP > Printing String Gradually!

    Hi Folks,

    I'm an undergrad at National University of Singapore. Just have a slight trouble with my current assignment.

    How do you gradually print a string say "ABCDEFGH", and you want it to print like:



    A
    AB
    ABC
    ABCD
    ABCDE
    ABCDEF
    ABCDEFG
    ABCDEFGH



    Many thanks!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm not going to hand out code on a silver platter just because you're from my alma mater

    Start by writing a program that given a string such as "ABCDEFGH", prints the first n characters of the string, where n is some suitable number, e.g., 3. Now, think of how you can write a loop to vary n from 1 to the number of characters in the string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Quote Originally Posted by laserlight View Post
    I'm not going to hand out code on a silver platter just because you're from my alma mater

    Start by writing a program that given a string such as "ABCDEFGH", prints the first n characters of the string, where n is some suitable number, e.g., 3. Now, think of how you can write a loop to vary n from 1 to the number of characters in the string.
    I already thought of a simple loop:

    Code:
    for(i=0; i<strlen(text); i++)
    {
       printf("%s\n", text[i]);
    }
    But it doesn't work.

    HELP!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you change %s to %c, that should work, except that it would only print the entire input string. As I implied earlier, you should use nested loops.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Quote Originally Posted by laserlight View Post
    If you change %s to %c, that should work, except that it would only print the entire input string. As I implied earlier, you should use nested loops.
    Mhmm... 2 loops...

    Code:
    for(i=0; i<strlen(text); i++)
    {
       for(j=0; j<strlen(text); j++)
       {
          printf("%c\n", text[j])
        }
    }
    This gives me

    [code]
    A
    B
    C
    D
    E
    F
    G
    A
    B
    C
    D
    E
    F
    G
    .
    .
    .
    .
    etc

    The issue that I have is, I have no idea how do I print A, then AB, then ABC. And my string could be say, a 100 long scary string. Z.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Inneart
    The issue that I have is, I have no idea how do I print A, then AB, then ABC.
    You're close. You don't have to use strlen(text) in both loops, and you do not have to print a newline immediately after printing each character. (Actually, you should save the result of strlen(text) and use it just once, but details of the reason behind this will be covered in CS1102.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Quote Originally Posted by laserlight View Post
    You're close. You don't have to use strlen(text) in both loops, and you do not have to print a newline immediately after printing each character. (Actually, you should save the result of strlen(text) and use it just once, but details of the reason behind this will be covered in CS1102.)
    Is this it? Hehe.

    Code:
    for(i=0; i<strlen(text)+1; i++)
    {
      for(j=0; j<i; j++)
      {
         printf("%c", text[j]);
       }
      printf("\n");
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Almost. i should begin from 1 instead of 0, and then instead of adding 1 to strlen(text), just use <= to compare.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Quote Originally Posted by laserlight View Post
    Almost. i should begin from 1 instead of 0, and then instead of adding 1 to strlen(text), just use <= to compare.
    Thanks laserlight. You are a great help. Bet you got A+ for all your programming modules. Thanks a mil!

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Getting closer... it's in the way you end your loops...

    Code:
    for(i=0; i<strlen(text); i++)
         { for(j=0; j<i; j++)
              printf("%c", text[j]);
            printf("\n"); }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, CommonTater's idea should also work, but I think that it is more intuitive to change the outer loop with the idea that it controls the number of characters that you intend to print on each line.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    if:
    width was the number of your letters to be printed.

    i was the number of char's you had printed on any one row.

    j was the number of char's you needed to print on any one row.

    set j=1.

    Now you're ready for your two nested loops:

    1) do loop while incremented j is less than width - the outer loop
    2) inner for i equal 0 i less than j increment i - the inner loop

    It's very intuitive, short and draws beautifully. Doesn't matter how many letters there are. Just set width variable to what you want.


    Here, I used it to print just one char: @ printing different char's is also easy though.

    Code:
    @
    @@
    @@@
    @@@@
    @@@@@
    @@@@@@
    @@@@@@@
    @@@@@@@@
    @@@@@@@@@
    @@@@@@@@@@
    @@@@@@@@@@@
    
    			    press enter when ready
    Last edited by Adak; 09-28-2010 at 05:06 PM.

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Thanks guys. Really appreciated it.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    char *text = "ABCDEFGH";
    
    for (i = 1; i <= strlen(text); i++)
        printf("%.*s\n", i, text);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM