-
printing long text
I am trying to print a sting. I want 50 char a line and i don't want the word at the end of the line to break....
I can't figure it out!!!
Here is what I have so far
int main(void)
{
const char text[]="This is a text I am trying to print this long\
text without having to break a word.";
int n;
while (text!=EOF)
{
n=strlen(text);
if (n>50)
{
n--;
while (text[n]!=' ')
{
continue;
}
text[n]='\n';
text[n+1]='\0';
}
printf("%s",text);
}
return 0;
}
-
hmm
I already posted this in the "word wrap" thread
here...
if you're looking for wordwrap in console,
look here.....
Code:
void mywordwrap(FILE *fp, char string[], short maxLineWidth) {
short startIndex;
short endIndex;
short actualLineWidth;
if (fp == NULL || string == NULL || maxLineWidth <= 0) {
return;
}
startIndex = 0;
endIndex = strlen(string);
while (startIndex < endIndex) {
while (startIndex < endIndex && isspace( string[startIndex] )) {
++startIndex;
}
actualLineWidth = maxLineWidth;
if (endIndex - startIndex > maxLineWidth) {
while (actualLineWidth > 0 &&
!isspace( string[ startIndex+actualLineWidth-1 ]) &&
!isspace( string[ startIndex+actualLineWidth ]) ) {
--actualLineWidth;
}
}
if (startIndex < endIndex && actualLineWidth > 0) {
fprintf(fp,"%.*s\n",actualLineWidth, &string[ startIndex ]);
}
startIndex += actualLineWidth;
}
}
//syntax mywordwrap(stdout, chararray, 70);
put the above function above your main function,
in main(), you have
char text[]="This is a text I am trying to print this long
text without having to break a word.";
you said you wanted a 50 char line....
so do this
mywordwrap(stdout, text, 50)
got it now ?
GOOD LUCK
-
thanks that is nice of you but I am a beginner C This is a homework problem....I need something much simpler
I tried to use printf("%0.50s",text); it works but it prints only one line
-
There is no "simple" way to do this. The only way to do it is:
Code:
count the caracters in a word
if the total characters + the word length + 1 is >= X
print a new line
total characters = 0
print the word
total character += word length + 1
print a space
Put that in a loop and loop until you run out of words.
Quzah.
-
quzah I hope you are still online
I am having trouble writting the codes for your instructions...I need help
This is what I have so far:
int main(void)
{
const char text[]="This is a text I am trying to print this long\
text without having to break a word.";
//count the caracters in a word
//if the total characters + the word length + 1 is >= X
// print a new line
// total characters = 0
//print the word
//total character += word length + 1
//print a space
int k;
for (k=0;k<strlen(text);k++)
{
if (((strlen(text)) + (word length + 1))>=60)
printf("\n");
printf(" ");
}
return 0;
}
-
ok dude...
here's a clue...
1) Take a list of all the places in the sentence where
there are spaces
2) find the nearest space to 50 (if you want to allow 50 characters
per line)
3) use a for loop to print till that place
4) loop it again for the whole sentence
Code:
int array[50]; //holds places where space character is in the given string
for (i = 0; i<strlen(text); i++)
{
if (text[i] == ' ')
{
array[k] = i+1;
printf("%d\n", array[k]);
k++;
}
}
The above code will give you wherever there are spaces in
a given string.
Try working the rest of it for yourself...
hope this worked as a clue for you..
-
something like this will probably work fine:
Code:
int spaceplace,counter=0,printcounter=0;
char sentence;
....
....
while(couner<strlen(sentence))
{
while(counter<25)
{
if(sentence[counter]=' ')
spaceplace = counter;
counter++;
}
for(printcounter=0;printcounter<spaceplace;printcounter++)
printf("%c",sentence[printcounter]);
printf("\n");
counter=printcounter+1;
}
I think somethin like that will work, or atleast give you an idea