a program that will display teh string "HELLOWORLD" in ascending style

#include<stdio.h>
#include <conio.h>

void main()
char str[]="HELLOWORLD"
int i. choice;
while(1)
{
  clrscr();
  printf("(1) Ascending style\n");
  printf("(2) Exit\n");
  printf("enter your choice:");
  scanf("%d", &choice);
  printf("\n\n");
  switch(choice)
  {
  case 1: [B]for (i=0; str[i]!='\0';i++)   //-->  Please!!! this is where i need
            printf("%.*s\n",i,str);          //--> help..how does this work? what
    printf("%.*s\n",i,str);          //--> does   .* stand for??? please
    getch();                     // i need help....how is descending style??
    break;
    [/B] case 2: exit(1);
  default: printf(\n\nInvalid choice\n\n");
  }

  getch();
}

Recommended Answers

All 7 Replies

>display teh string "HELLOWORLD" in ascending style
Riiight. Care to explain what you mean by "ascending style"?

>what does %.*s stand for???
The . is a field width modifier. Any value after it is how many characters of the string should be printed. The * is a placeholder for a value. In your call, * is replaced with the value of i. This may help you to understand a bit better:

#include <stdio.h>

int main ( void )
{
  printf ( "|%.*s|\n", 5, "helloworld" );
  return 0;
}

ascending order:

H
HE
HEL
HELL
HELLO
HELLOW
HELLOWO
HELLOWOR
HELLOWORL
HELLOWORLD

*this is the output. please help me in finding out how to make it in descending order. many thanks!!!! :D

Just reverse the counter so that it goes from back to front instead of front to back:

#include <stdio.h>

int main ( void )
{
  const char *p = "HELLOWORLD";
  int i;

  for ( i = 10; i >= 0; i-- )
    printf ( "%.*s\n", i, p );

  return 0;
}

im sorry...but how can i do it using the previous program i sent --> using arrays(string arrays??) by putting another case in the switch statement which prints the descending "Helloworld"... how do you properly insert it in the program?
in other words, how can i reverse Case 1 ?

so much thanks for being patient with me!! :)
btw, thanks for the examples.. " . " and " * " used here are new to me

e.g

1) Ascending Stlye
2) Decending Stlye
3) Exit

>but how can i do it using the previous program i sent
I'm not going to do your homework for you. My code serves as a template that you can use to figure out the solution to your problem.

just one last thing...

for (i=0; str!='\0';i++)
printf("%.*s\n",i,str);
printf("%.*s\n",i,str);
getch();
break;

i just need an explaination on how this works(e.g why set final value to '\0' and why are there two printf's, etc) thats all i need to know and i will be able to create the descending style using this similar case

thanks again!

>why set final value to '\0'
Because that's the definition of a string in C. A sequence of zero or more characters terminated by a null character ('\0').

>and why are there two printf's
On is inside the loop and prints all but the complete string, and the last is outside the loop and prints the complete string. You can print the value of i to see why:

#include <stdio.h>

int main()
{
  char *p = "HELLOWORLD";
  int i;

  for ( i = 1; p[i] != '\0'; i++ )
    printf ( "%d\t%.*s\n", i, i, p );
  printf ( "%d\t%.*s\n", i, i, p );

  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.