String Manipulation

A

aloha826

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.
 
K

Keith Thompson

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.
 
A

Antoninus Twink

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;
}
 
R

Richard

Antoninus Twink said:
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!
 
D

Default User

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top