print a pascal type char pattern

Reply

Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

print a pascal type char pattern

 
0
  #1
Jun 30th, 2009
i wish to print something like this
******** r
******* er
ter
uter
puter
mputer
omputer
computer
where * is a blank space for each line
i used array of strings
String []s1=new String [20]
for 5 blank spaces i used s1+" " + " "..+(5)""
then 4 blank spaces and print required charcters
i want to know how i can put it in a good logical general code
Last edited by akulkarni; Jun 30th, 2009 at 4:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: print a pascal type char pattern

 
0
  #2
Jun 30th, 2009
Originally Posted by akulkarni View Post
i wish to print something like this
******** r
******* er
ter
uter
puter
mputer
omputer
computer
where * is a blank space for each line
i used array of strings
String []s1=new String [20]
for 5 blank spaces i used s1+" " + " "..+(5)""
then 4 blank spaces and print required charcters
i want to know how i can put it in a good logical general code

If you use code tags when posting (even though it's not code), your spacing won't be stripped out.


[code]
// paste pattern here
[/code]


  1. r
  2. er
  3. ter
  4. uter
  5. puter
  6. mputer
  7. omputer
  8. computer


Use a nested for-loop:

  1. String theWord = "computer";
  2. int numLetters = theWord.length ();
  3.  
  4. for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--)
  5. {
  6. // display numSpaces spaces using for-loop.
  7. // display last numLetters - numSpaces letters of theWord.
  8. // display newline.
  9. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: print a pascal type char pattern

 
0
  #3
Jun 30th, 2009
i am not getting the blank space using for loop.i am worried i dont understand for properly can u please give me the entire program i will be grateful i know to print the letters but it goes left to right
r
er
ter
uter
puter
mputer
omputer
computer
i am not getting the blank spaces
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: print a pascal type char pattern

 
0
  #4
Jun 30th, 2009
Originally Posted by akulkarni View Post
i am not getting the blank space using for loop.i am worried i dont understand for properly can u please give me the entire program i will be grateful i know to print the letters but it goes left to right
r
er
ter
uter
puter
mputer
omputer
computer
i am not getting the blank spaces

Please post your results using code tags and post the program that you used to get those results. I can't give you the whole program. It's against the forum rules.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: print a pascal type char pattern

 
0
  #5
Jun 30th, 2009
  1. class gratefultodani
  2. {
  3. public static void main(String args[])
  4. {
  5. String s1="computer";
  6. /*reverse*/
  7. String s2=new StringBuffer(s1).reverse().toString();
  8. char []s3=s2.toCharArray();
  9. int i,j;
  10. for(i=0;i<=7;i++)//8 characters 8th is null
  11. {
  12. for(j=0;j<=i;j++)
  13. {
  14. System.out.print(s3[j]);
  15. }
  16. System.out.println();
  17. }
  18. }
  19. }
output
r
er
ter
uter
puter
mputer
omputer
computer
no clue about spaces; tabs,string =" ",'\0' all tried but for loop use gives me a weird output
Last edited by Tekmaven; Jul 1st, 2009 at 6:28 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: print a pascal type char pattern

 
0
  #6
Jun 30th, 2009
Originally Posted by akulkarni View Post
class gratefultodani
{
public static void main(String args[])
{
String s1="computer";
/*reverse*/
String s2=new StringBuffer(s1).reverse().toString();
char []s3=s2.toCharArray();
int i,j;
for(i=0;i<=7;i++)//8 characters 8th is null
{
for(j=0;j<=i;j++)
{
System.out.print(s3[j]);
}
System.out.println();
}
}
}
output
r
er
ter
uter
puter
mputer
omputer
computer
no clue about spaces; tabs,string =" ",'\0' all tried but for loop use gives me a weird output

Code tags!


[code]
// paste code or pattern here
[/code]




  1. class gratefultodani
  2. {
  3. public static void main(String args[])
  4. {
  5. String s1="computer";
  6. /*reverse*/
  7. String s2=new StringBuffer(s1).reverse().toString();
  8. char []s3=s2.toCharArray();
  9. int i,j;
  10. for(i=0;i<=7;i++)//8 characters 8th is null
  11. {
  12. for(j=0;j<=i;j++)
  13. {
  14. System.out.print(s3[j]);
  15. }
  16. System.out.println();
  17. }
  18. }
  19. }

Line 10 - Null terminator in a String is a C concept. Don't worry about it in Java.

I don't understand what your are "reversing" and why. You don't need to reverse anything. You could, of course, since there's more than one way of doing this, but you don't have to.

You should post the desired output, the actual output, and the code itself. Post ALL of this in code tags.

In your loop, there is no attempt to display a space. that's why you don't have any spaces. You don't need a character array. You have your string and that's good enough:

  1. String theWord = "computer";
  2. int numLetters = theWord.length ();
  3.  
  4. for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--)
  5. {
  6. for (int i = numLetters - 1; i >= 0; i--)
  7. {
  8. System.out.print (" ");
  9. }
  10. for (int i = numSpaces; i < numLetters; i++)
  11. {
  12. System.out.print (theWord.charAt (i));
  13. }
  14. System.out.println ();
  15. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: print a pascal type char pattern space reduce by 1 after every line

 
0
  #7
Jun 30th, 2009
[QUOTE=VernonDozier;904502]Code tags!


[code]
your code gives me space thats fine but it should reduce by 1 after every line
your program gives

r
er
ter
uter
puter



desired output
r
er
ter
uter
puter







/code]




[code=JAVA]
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: print a pascal type char pattern space reduce by 1 after every line

 
0
  #8
Jul 1st, 2009
  1.  
  2.  
  3. i am not able to express my desired output
  4. i dont see it in this window the way i want
  5. the space should reduce by 1 after every line and then print the characters
Last edited by akulkarni; Jul 1st, 2009 at 12:01 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: print a pascal type char pattern space reduce by 1 after every line

 
0
  #9
Jul 1st, 2009
DON'T use my noparse tags. Those were there only to show you how to use code tags. Do exactly what is on the screen and nothing else.


[code]
// paste pattern here
[/code]


Is this what you were trying to post?

  1. your code gives me space thats fine but it should reduce by 1 after every line
  2. your program gives
  3.  
  4. r
  5. er
  6. ter
  7. uter
  8. puter
  9.  
  10.  
  11.  
  12. desired output
  13. r
  14. er
  15. ter
  16. uter
  17. puter


Use the "Preview Post" button instead of "Submit Reply" and change as needed till it's right. Then submit your post.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: print a pascal type char pattern

 
0
  #10
Jul 1st, 2009
Originally Posted by VernonDozier View Post
Code tags!


[code]
// paste code or pattern here
[/code]




  1. class gratefultodani
  2. {
  3. public static void main(String args[])
  4. {
  5. String s1="computer";
  6. /*reverse*/
  7. String s2=new StringBuffer(s1).reverse().toString();
  8. char []s3=s2.toCharArray();
  9. int i,j;
  10. for(i=0;i<=7;i++)//8 characters 8th is null
  11. {
  12. for(j=0;j<=i;j++)
  13. {
  14. System.out.print(s3[j]);
  15. }
  16. System.out.println();
  17. }
  18. }
  19. }

Line 10 - Null terminator in a String is a C concept. Don't worry about it in Java.

I don't understand what your are "reversing" and why. You don't need to reverse anything. You could, of course, since there's more than one way of doing this, but you don't have to.

You should post the desired output, the actual output, and the code itself. Post ALL of this in code tags.

In your loop, there is no attempt to display a space. that's why you don't have any spaces. You don't need a character array. You have your string and that's good enough:

  1. String theWord = "computer";
  2. int numLetters = theWord.length ();
  3.  
  4. for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--)
  5. {
  6. for (int i = numLetters - 1; i >= 0; i--)
  7. {
  8. System.out.print (" ");
  9. }
  10. for (int i = numSpaces; i < numLetters; i++)
  11. {
  12. System.out.print (theWord.charAt (i));
  13. }
  14. System.out.println ();
  15. }

My bad, I think. I'm not sure whether I made a typo before or got confused or whether I 100% understood what the goal of the program was, but if THIS is the goal:

  1. r
  2. er
  3. ter
  4. uter
  5. puter
  6. mputer
  7. omputer
  8. computer

then line 6 needs to be changed to this:

  1. String theWord = "computer";
  2. int numLetters = theWord.length ();
  3.  
  4. for (int numSpaces = numLetters - 1; numSpaces >= 0; numSpaces--)
  5. {
  6. for (int i = numLetters - 1; i >= 0; i--)
  7. {
  8. System.out.print (" ");
  9. }
  10. for (int i = numSpaces; i < numLetters; i++)
  11. {
  12. System.out.print (theWord.charAt (i));
  13. }
  14. System.out.println ();
  15. }

  1. for (int i = numSpaces - 1; i >= 0; i--)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC