-5

I want to print stars before and after a name, like, ****Johannah****.

WITHOUT using the "for" loop, using total and complete logic.

The number of stars to be printed should be entered by the user and then the stars should be before and after the name in the code as the output.

For ex:" Enter the name: Johannah.
Enter the number of stars to be printed before and after the name: 5.
Name: *****Johannah*****

Please help. Thanks.

Rohit
  • 9
  • 3
  • 3
    This smells a lot like a homework assignment. You should read this (http://meta.stackoverflow.com/questions/253792/stack-overflow-and-homework-questions) and then you should write some code. After you have done that, feel free to come back if your code doesn't work and I'm sure someone will assist you. – David Hoelzer Jan 25 '17 at 10:56
  • 2
    Can u show us your efforts ,what you did? – minigeek Jan 25 '17 at 10:57
  • 1
    No, this isn't a homework at all. I'm just trying to do different things that will help me crack my brain to a further extent. It's not a homework. – Rohit Jan 25 '17 at 11:01
  • 1
    *"I'm just trying to do different things"* - You should post your attempts in the question then – StoryTeller - Unslander Monica Jan 25 '17 at 11:05
  • 1
    `char stars[] = "********************************"; printf("%.*s\n", 5, stars);` – LPs Jan 25 '17 at 11:07
  • @DavidHoelzer Isn't This question good , i found it interesting , without using loop printing * – Suraj Jain Jan 26 '17 at 04:22
  • @LPs Thanks A Lot , learned something new and was able to answer because of you , can you see the below answer and tell me if i did it right. –  Jan 26 '17 at 06:09
  • @Stranger Yes, I saw. I already upvoted. ;) – LPs Jan 26 '17 at 07:02
  • @LPs Why it is put on hold , it is an extremely good question and not broad , perfect to the point. –  Jan 26 '17 at 08:21
  • @Stranger The question is to broad due to the fact that there are many solutions to the question. Moreover OP is not showing efforts and ask for a complete code solution, that is OT for SO. In other words: it smells like homework. ;) – LPs Jan 26 '17 at 08:23
  • I only found one solution to this problem by far and I've appreciated it from both your sides. This doesn't mean I'm not showing any efforts just because I'm not posting here. I already wrote to @Stranger 's answer that it worked. Peace :) – Rohit Jan 26 '17 at 08:25
  • @LPs Just for my own knowledge can you give me other solutions , i cannot literally think any other. It would greatly enhance my knowledge , i am banned to ask question here , because some people down voted my question without telling why they did so . –  Jan 26 '17 at 08:44
  • Same here, @LPs – Rohit Jan 26 '17 at 08:45
  • @Stranger Off the top of my head: use `memcpy`/`strncpy` to create a new c string to print.. – LPs Jan 26 '17 at 08:45
  • But how does that help in the n(no of stars) variable to print that many amount of stars? @LPs – Rohit Jan 26 '17 at 08:47
  • @Stranger and Rohit [This one](http://ideone.com/EH0YpT) can be something different, for example. – LPs Jan 26 '17 at 09:39
  • @Rohit See Above. –  Jan 26 '17 at 10:33

1 Answers1

1

You Could Well Do This First Make a char array , Containing Number Of Maximum Stars that will be asked to print.

 char stars[] = "***************************************";

Then Ask User Input for number of stars they want to print.

 int a;
 printf("Enter Number of Stars You Want To Print : ");
 scanf("%d" , &a);

Then You print it using

 printf("%.*s\n" , a , stars);

What is basically happening is that you are setting variable width by using %.*s , here * is taking a , for example if a = 5 then it turns out to be %.5s and then it print first 5 characters of string.

The catch here is you should know maximum number of stars that you would be asked to print so that you can initialize the character array according to your need.

Please See These Question To Know More How to print only certain parts of a string? and Set variable text column width in printf .

miken32
  • 42,008
  • 16
  • 111
  • 154