M
Mike Polinske
I am new to the C programming language but have been programming in
Cobol for over 10 years. When I compile the following code, it
compiles clean but I get an application error both under Windows XP and
Win2K.
#include <stdio.h>
#include <ctype.h>
char title[] = "Year End Report";
char work_buffer[51];
main()
{
strcpy(work_buffer,title);
strncenter(work_buffer,50);
printf("%s\n",work_buffer);
strljust(work_buffer);
printf("%s\n",work_buffer);
strrjust(work_buffer);
printf("%s\n",work_buffer);
}
/* Left justify a string */
strljust(str)
char *str;
{
int len;
len = strlen(str);
while (isspace(str) != 0) {
memmove (str,str + 1,len);
str[len] = ' ';
}
}
/* Reverse a string */
strrev(str)
char *str;
{
char ch;
char *end;
end = str + strlen(str) - 1;
while (str < end) {
ch = *end;
*end-- = *str;
*str++ = ch;
}
}
/* Right justify a string */
strrjust(str)
char *str;
{
strrev(str);
strljust(str);
strrev(str);
}
/* Truncate to last non-white character */
strtrunc(str)
char *str;
{
char *end;
end = str + strlen(str) - 1;
while ((*str != 0) && (isspace (*end) != 0)) {
*end-- = 0;
}
}
/* Pad a string for len with spaces */
struntrunc(str,len)
char *str;
int len;
{
while (strlen (str) < len) {
strcat (str,' ');
}
}
/* Center a string */
strcenter(str)
char *str;
{
strncenter (str, strlen(str));
}
/* Center a string within width */
strncenter(str,width)
char *str;
int width;
{
int non_blank_len, padding;
strtrunc (str);
strrev (str);
strtrunc (str);
non_blank_len = strlen (str);
padding = (width - non_blank_len) / 2;
struntrunc (str,padding);
strrev (str);
struntrunc (str,width);
}
It seems to be blowing up in the struntrunc function, but I can't
understand why. I know that this program is trying to take a string,
center it, left justify it and right justify it.
Any ideas on how I can get this program to run?
I am using Open Watcom C/C++ 1.4 but I had the same problem when I
tried it with OW 1.3.
Thanks
Cobol for over 10 years. When I compile the following code, it
compiles clean but I get an application error both under Windows XP and
Win2K.
#include <stdio.h>
#include <ctype.h>
char title[] = "Year End Report";
char work_buffer[51];
main()
{
strcpy(work_buffer,title);
strncenter(work_buffer,50);
printf("%s\n",work_buffer);
strljust(work_buffer);
printf("%s\n",work_buffer);
strrjust(work_buffer);
printf("%s\n",work_buffer);
}
/* Left justify a string */
strljust(str)
char *str;
{
int len;
len = strlen(str);
while (isspace(str) != 0) {
memmove (str,str + 1,len);
str[len] = ' ';
}
}
/* Reverse a string */
strrev(str)
char *str;
{
char ch;
char *end;
end = str + strlen(str) - 1;
while (str < end) {
ch = *end;
*end-- = *str;
*str++ = ch;
}
}
/* Right justify a string */
strrjust(str)
char *str;
{
strrev(str);
strljust(str);
strrev(str);
}
/* Truncate to last non-white character */
strtrunc(str)
char *str;
{
char *end;
end = str + strlen(str) - 1;
while ((*str != 0) && (isspace (*end) != 0)) {
*end-- = 0;
}
}
/* Pad a string for len with spaces */
struntrunc(str,len)
char *str;
int len;
{
while (strlen (str) < len) {
strcat (str,' ');
}
}
/* Center a string */
strcenter(str)
char *str;
{
strncenter (str, strlen(str));
}
/* Center a string within width */
strncenter(str,width)
char *str;
int width;
{
int non_blank_len, padding;
strtrunc (str);
strrev (str);
strtrunc (str);
non_blank_len = strlen (str);
padding = (width - non_blank_len) / 2;
struntrunc (str,padding);
strrev (str);
struntrunc (str,width);
}
It seems to be blowing up in the struntrunc function, but I can't
understand why. I know that this program is trying to take a string,
center it, left justify it and right justify it.
Any ideas on how I can get this program to run?
I am using Open Watcom C/C++ 1.4 but I had the same problem when I
tried it with OW 1.3.
Thanks