String Streams

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stroumfs
    New Member
    • Mar 2007
    • 28

    String Streams

    Hello everyone...

    I have the following problem and i have no idea how to do it.
    Can anyone help?

    Write a procedure that takes a string as its parameter and which prints out a pattern made out of the string. For example, given the string "XMAS", it would output the following:

    X
    MM
    AAA
    SSSS
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    You might like to look under the Articles section for c/c++. I believ AdrianH has posted some tutorials how to make patterns from asterisks. I would imagine that would help you to get started, and give you an idea of how to create the pattern!

    Comment

    • Stroumfs
      New Member
      • Mar 2007
      • 28

      #3
      Originally posted by Stroumfs
      Hello everyone...

      I have the following problem and i have no idea how to do it.
      Can anyone help?

      Write a procedure that takes a string as its parameter and which prints out a pattern made out of the string. For example, given the string "XMAS", it would output the following:

      X
      MM
      AAA
      SSSS
      I am using C++ and compiler DevC++.



      thanks

      Comment

      • Stroumfs
        New Member
        • Mar 2007
        • 28

        #4
        I am trying to find the tutorial that you are talking but i can't can you give me some info or the link please?


        thnx

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          Hi,
          You can write a function which takes the character to print and the position of the character and you can print the character that many number of times.

          <code removed as per posting guidelines>

          Since this is small part i have posted the code....if it against posting guidelines the sorry about that and please remove it.

          Raghuram

          Comment

          • DeMan
            Top Contributor
            • Nov 2006
            • 1799

            #6
            try here (cleverly titled something about asterix so you would automatically assume Goscinny and Udzero ;))

            Comment

            • Stroumfs
              New Member
              • Mar 2007
              • 28

              #7
              Hello everyone!

              thank you for your replies..i tried by myself and i wrote the following piece of code. But i can't compile it. Can anyone tell me what is wrong with my code?
              thnx

              [code=cpp]#include <iostream>

              using namespace std;
              int main()
              {

              int counter,i,y;
              char text[30];
              cout << "Please enter a string : ";
              int couter =0;
              while ((text[counter]!= '\n') && (counter<30)){
              cin>> text[counter++];
              }
              for (i=0;i<counter; i++)
              {
              for(y=0;y<=i;y+ +)
              {
              cout << text[i];
              }
              cout << '\n' ;
              }
              }[/code]
              Last edited by AdrianH; Jun 9 '07, 10:49 AM. Reason: Please use [code=cpp][/code] tags to improve readability. Also INDENT YOUR CODE!

              Comment

              • Savage
                Recognized Expert Top Contributor
                • Feb 2007
                • 1759

                #8
                Originally posted by DeMan
                try here (cleverly titled something about asterix so you would automatically assume Goscinny and Udzero ;))
                Ehm,is there a problem with the title.

                PS:I forgot to ask:

                Can u,or someone else, please change it to asterisks?

                Savage

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  To print this:

                  X
                  MM
                  AAA
                  SSSS

                  using an array just recognize that the number of times to display the character is the array element number + 1.

                  Just write an outer loop that counts from 0 to the end of the array

                  Then write an inner loops that runs from 0 to the count+1 of the outer loop. You display the character in this inner loop.

                  Comment

                  • Stroumfs
                    New Member
                    • Mar 2007
                    • 28

                    #10
                    I am sorry but i don't understand you. I think that i have done what you suggest in my code. and still i don't understand why my code doesn't work.it seems ok to me.

                    Can you be more specific please...

                    thx

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      This code:
                      [code=cpp]
                      cout << text[i];
                      [/code]

                      maybve should be
                      [code=cpp]
                      cout << text[y];
                      [/code]

                      Here's what I did:
                      [code=cpp]
                      char msg[] = "XMAS";
                      for(int i =0; i < 4; ++i)
                      {
                      for(int j = 0; j < i + 1;++j)
                      {
                      cout << msg[i];
                      }
                      cout <<endl;
                      }
                      [/code]

                      which is what I think you meant to do.

                      Comment

                      • DeMan
                        Top Contributor
                        • Nov 2006
                        • 1799

                        #12
                        Originally posted by Savage
                        Ehm,is there a problem with the title.

                        PS:I forgot to ask:

                        Can u,or someone else, please change it to asterisks?

                        Savage
                        NO problem with your title......Sorr y, I thought you had concealed a joke in there (and I can't change it - i don't have my magic powers in here)

                        BTW: I'm terribly sorry, I gave AdrianH credit for that one......you can kill me next time.....

                        Comment

                        • Savage
                          Recognized Expert Top Contributor
                          • Feb 2007
                          • 1759

                          #13
                          Originally posted by DeMan
                          NO problem with your title......Sorr y, I thought you had concealed a joke in there (and I can't change it - i don't have my magic powers in here)

                          BTW: I'm terribly sorry, I gave AdrianH credit for that one......you can kill me next time.....
                          NO problem!!

                          I did concealed a joke,but maybe someone finds it stupid..

                          Savage

                          Comment

                          • AdrianH
                            Recognized Expert Top Contributor
                            • Feb 2007
                            • 1251

                            #14
                            Originally posted by DeMan
                            You might like to look under the Articles section for c/c++. I believ AdrianH has posted some tutorials how to make patterns from asterisks. I would imagine that would help you to get started, and give you an idea of how to create the pattern!
                            No, that was Savage.


                            Adrian

                            Comment

                            • AdrianH
                              Recognized Expert Top Contributor
                              • Feb 2007
                              • 1251

                              #15
                              Originally posted by Savage
                              NO problem!!

                              I did concealed a joke,but maybe someone finds it stupid..

                              Savage
                              Are you referring to the Asterix comix? I loved those. :)


                              Adrian

                              Comment

                              Working...