String Manipulation

Discussion in 'C Programming' started by aloha826@gmail.com, Jan 6, 2009.

  1. Guest

    Hi,
    I need to break a string into few words in case of exceeding 16 chars
    at the last occurance of space
    For example:
    the string has:
    "This is just only for testing"
    with limit of 16 chars, it reach: "This is just onl"
    so, I need to break it to next line at the last occurance of space of
    the 16 chars:
    so it will be:
    "This is just"
    "only for testing"
    if the 2nd line is exceeding 16 chars, it will break at last
    occurance
    of space, and move to next line
    can anyone provide this function ?
    Regards.
     
    , Jan 6, 2009
    #1
    1. Advertising

  2. "" <> writes:
    > I need to break a string into few words in case of exceeding 16 chars
    > at the last occurance of space
    > For example:
    > the string has:
    > "This is just only for testing"
    > with limit of 16 chars, it reach: "This is just onl"
    > so, I need to break it to next line at the last occurance of space of
    > the 16 chars:
    > so it will be:
    > "This is just"
    > "only for testing"
    > if the 2nd line is exceeding 16 chars, it will break at last
    > occurance
    > of space, and move to next line
    > can anyone provide this function ?


    Why do you need to do this? If this is a homework assignment (which,
    quite frankly, is what it looks like), then we're not going to do it
    for you, but if you post some code we'd be willing to help.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 6, 2009
    #2
    1. Advertising

  3. On 6 Jan 2009 at 19:41, wrote:
    > I need to break a string into few words in case of exceeding 16 chars
    > at the last occurance of space
    > For example:
    > "This is just only for testing"


    #include <stdio.h>

    int main(void)
    {
    FILE *fp;
    char buf[18];
    if(fp = popen("fmt -w 16", "w")) {
    fputs("This is only just for testing", fp);
    while(fgets(buf, sizeof buf, fp))
    fputs(buf, stdout);
    pclose(fp);
    }
    return 0;
    }
     
    Antoninus Twink, Jan 6, 2009
    #3
  4. Richard Guest

    Antoninus Twink <> writes:

    > On 6 Jan 2009 at 19:41, wrote:
    >> I need to break a string into few words in case of exceeding 16 chars
    >> at the last occurance of space
    >> For example:
    >> "This is just only for testing"

    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > FILE *fp;
    > char buf[18];
    > if(fp = popen("fmt -w 16", "w")) {
    > fputs("This is only just for testing", fp);
    > while(fgets(buf, sizeof buf, fp))
    > fputs(buf, stdout);
    > pclose(fp);
    > }
    > return 0;
    > }
    >


    Naughty!

    --
    I'm not a person who particularly had heros when growing up.
    - Dennis Ritchie when asked about the hero worship coming from c.l.c
     
    Richard, Jan 6, 2009
    #4
  5. Default User Guest

    wrote:

    > Hi,
    > I need to break a string into few words in case of exceeding 16 chars
    > at the last occurance of space
    > For example:
    > the string has:
    > "This is just only for testing"
    > with limit of 16 chars, it reach: "This is just onl"
    > so, I need to break it to next line at the last occurance of space of
    > the 16 chars:
    > so it will be:
    > "This is just"
    > "only for testing"
    > if the 2nd line is exceeding 16 chars, it will break at last
    > occurance
    > of space, and move to next line
    > can anyone provide this function ?
    > Regards.


    Eric gave you some good advice. I solved this problem in a more general
    case for my text-adventure game. I had a function that took in the
    string, the width, and the starting column.

    It would then print the string based on those parameters, returning the
    column number of the final character. That, coupled with the start
    parameter, allowed me to concatenate multiple strings into one seamless
    paragraph for display purposes.

    The width parameter allows users to set the display width they prefer
    (default to 80).




    Brian
     
    Default User, Jan 6, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jared in ecs

    Perl string manipulation

    jared in ecs, Oct 22, 2003, in forum: Perl
    Replies:
    2
    Views:
    823
    Roy Johnson
    Oct 22, 2003
  2. Garfield

    String Array Manipulation Problem

    Garfield, Aug 27, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,837
    Michael Lang
    Aug 27, 2003
  3. Aaron

    String Manipulation

    Aaron, Dec 31, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,664
    Dave Davidson
    Dec 31, 2003
  4. Aaron

    Advanced String Manipulation (C#)

    Aaron, Jan 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    860
  5. Chris

    String manipulation

    Chris, May 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    467
    Pete Wright
    May 24, 2004
Loading...

Share This Page