C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-11-2008, 05:37 AM   #1
Registered User
 
Join Date: Jul 2008
Posts: 2
how to print a increasing number of words?

hello,
im looking for a refrence to learn from and/or help with the algorithm.
i have a homework assignment where i recieve a string and then i have to print a incresaing amount of its words, like this:

input: this is a example
output: this
this is
this is a
this is a example

im allowed to loop across the string (which is a max of 100 chars) only once.
my idea is to:
receive the string and save it in a fixed array.
allocate memory and save the string in it.
call a function that replaces ' ' with '/0' .
count how many times '/0' appears (=n).
for (i=0; i<n; i++)
use scanf i times to read the string in to a nulled array-(i dont know how to do this)
print the scaned string.
i++

have you got a easier way to do this or any advice?
thanks a lot,
gershon
gershonj is offline   Reply With Quote
Old 07-11-2008, 05:53 AM   #2
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
I would use strchr to find each space in the string.
From every space, print the entire string, beginning from the beginning, to the place where strchr found a space.
Then use strchr again to find the next space and repeat.

I'm assuming that...
Quote:
im allowed to loop across the string (which is a max of 100 chars) only once.
...means that you are allowed to use a loop inside a loop, but not do two loops after each other.
Otherwise this won't work.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-11-2008, 06:22 AM   #3
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Alternatively, you can do it with just one loop, if you copy the original string into a new string, and temporarily end the string with a zero when you find a space, then print string "as far as it is now" (followed by a newline) and continue copying/scanning.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 07-11-2008, 06:26 AM   #4
Registered User
 
gavra's Avatar
 
Join Date: Jun 2008
Posts: 208
one loop + one variable + one variable
I think it's enough.
__________________
gavra.
gavra is offline   Reply With Quote
Old 07-11-2008, 07:36 AM   #5
Registered User
 
Join Date: Jul 2008
Posts: 2
yep, i think i got it :)

thanks a lot for your help!
i think that what ill do is a loop inside a loop for printing one char at a time. the loop will be updated by the number of '/0' chars that it will incounter (they will be put inside the code by a function.
have a gerat weekend,
gershon
gershonj is offline   Reply With Quote
Reply

Tags
allocated memory, loop, printf, scanf, string

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
memory issue t014y C Programming 2 02-21-2009 12:37 AM
What kind of programs should I start writing? Macabre C++ Programming 23 04-12-2003 08:13 PM
detecting the number of pages/documents to print on the print job? mickey C# Programming 0 03-28-2003 08:20 AM
counting the number of words in a file stuartbut C Programming 2 04-13-2002 08:19 AM


All times are GMT -6. The time now is 10:29 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22