>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;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401