D
Dick D
Could someone tell me why i get an "access violation" exception when the
character at string == newline (0x10) and i try to replace it with a
space (0x20)? (see the get_word function)
Thanks in Advance.
#include <ctype.h>
#include <stdio.h> /* For FILE */
char *word_wrap(char *string, int line_len);
static int get_word(char *string); /* returns size of next word*/
static int tab_size = 5; /* size to consider tabs as */
char *word_wrap(char *string, int line_len)
{
int len, /* length of current word */
current_len = 0; /* current length of line */
int start_line = 0; /* index of beginning if line */
while (0 != (len = get_word(&string[current_len + start_line])))
{
printf("len = %d\n",len);
if (current_len + len < line_len)
current_len += len;
else
{
string[start_line+current_len] = '\n';
start_line += current_len + 1;
current_len = 0;
}
}
return string;
}
static int get_word(char *string)
{
register int i = 0, word_len = 0;
if (!string[0])
return 0;
printf("\n");
// **********************************************************//
// **********************************************************//
while (isspace(string))
{
if (string == '\n')
string = ' ';
// **********************************************************//
word_len++;
i++;
}
printf("%c",string);
while (string && !isspace(string[i++]))
{
printf("%c",string);
word_len++;
}
printf("\n");
return word_len;
}
main()
{
char *string =
"This is a long line\nto be wrapped by the w_wrap function. "
"Hopefully, things will work correctly and it will be wrapped
"
"between words. On the other hand, maybe I should hope that
it "
"doesn't work well so I will have an opportunity\nto learn
more "
"about what I'm doing";
printf("Here's a string wrapped to 40 columns:\n\n%s\n\n",
word_wrap(string, 40));
printf("And here it's wrapped to 72:\n\n%s\n\n",
word_wrap(string,72));
return 0;
}
character at string == newline (0x10) and i try to replace it with a
space (0x20)? (see the get_word function)
Thanks in Advance.
#include <ctype.h>
#include <stdio.h> /* For FILE */
char *word_wrap(char *string, int line_len);
static int get_word(char *string); /* returns size of next word*/
static int tab_size = 5; /* size to consider tabs as */
char *word_wrap(char *string, int line_len)
{
int len, /* length of current word */
current_len = 0; /* current length of line */
int start_line = 0; /* index of beginning if line */
while (0 != (len = get_word(&string[current_len + start_line])))
{
printf("len = %d\n",len);
if (current_len + len < line_len)
current_len += len;
else
{
string[start_line+current_len] = '\n';
start_line += current_len + 1;
current_len = 0;
}
}
return string;
}
static int get_word(char *string)
{
register int i = 0, word_len = 0;
if (!string[0])
return 0;
printf("\n");
// **********************************************************//
// **********************************************************//
while (isspace(string))
{
if (string == '\n')
string = ' ';
// **********************************************************//
word_len++;
i++;
}
printf("%c",string);
while (string && !isspace(string[i++]))
{
printf("%c",string);
word_len++;
}
printf("\n");
return word_len;
}
main()
{
char *string =
"This is a long line\nto be wrapped by the w_wrap function. "
"Hopefully, things will work correctly and it will be wrapped
"
"between words. On the other hand, maybe I should hope that
it "
"doesn't work well so I will have an opportunity\nto learn
more "
"about what I'm doing";
printf("Here's a string wrapped to 40 columns:\n\n%s\n\n",
word_wrap(string, 40));
printf("And here it's wrapped to 72:\n\n%s\n\n",
word_wrap(string,72));
return 0;
}