![]() |
| | #1 |
| Registered User Join Date: Jul 2008
Posts: 2
| how to print a increasing number of words? 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 | |
| | #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:
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:
| ||
| Elysia is offline | |
| | #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 | |
| | #4 |
| Registered User Join Date: Jun 2008
Posts: 208
| one loop + one variable + one variable I think it's enough.
__________________ gavra. |
| gavra is offline | |
| | #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 | |
![]() |
| Tags |
| allocated memory, loop, printf, scanf, string |
| Thread Tools | |
| Display Modes | |
|
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 |