how to print a increasing number of words?

This is a discussion on how to print a increasing number of words? within the C Programming forums, part of the General Programming Boards category; hello, im looking for a refrence to learn from and/or help with the algorithm. i have a homework assignment where ...

  1. #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

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    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...
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  4. #4
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    one loop + one variable + one variable
    I think it's enough.
    gavra.

  5. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  3. Replies: 0
    Last Post: 03-28-2003, 07:20 AM
  4. counting the number of words in a file
    By stuartbut in forum C Programming
    Replies: 2
    Last Post: 04-13-2002, 08:19 AM

Tags for this Thread


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