Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have codeblock like below i wanna get like an output
hello
ello
llo
lo
o

and I DON NOT want to add any other C statement. Where i did mistake?
char str[] = "hello";
char *p = str;
int i =0
for (i=0;i<5;i++)
{
printf("%c",*p++);
}

my current output is:
h
e
l
l
o


%s for scanf function, also for printf(), as clearly documented in MSDN and every book ever written about the C programming language.
Posted
Updated 29-Dec-09 11:27am
v4

printf("%s\n", p++);

[edit]OK, I messed up and forgot the newline; but, hey, nobody else noticed ;) [/edit]
 
Share this answer
 
v2
Mistakes include:
Missing a ; after int i =0.
You are only printing 1 char at a time
There is no line feed (as per you requirement)

Unfortunately you cannot fix this without adding another loop to count the chars to print. I would do it like this (using your restrictions):

char str[] = "hello";
int iLength = 5;

for (int i=0; i<iLength; i++)
{
	for (int j = i; j < iLength; j++)
		printf("%c", (int)str[j]);

	printf ("\r\n");
}
 
Share this answer
 
As already pointed out by Richard, you've to replace the statement
printf("%c", *p++);

that prints just the single character *p (see MSDN [^]) with the following one
printf("%s\n", p++);
that outputs a string starting from (increasing) address p.
:)
 
Share this answer
 
v3
Did you even try my suggestion? What is the point of asking a question, if your only response is to challenge what has been posted rather than modifying your code and actually seeing whether it works or not? :mad:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900