K
Keith Thompson
actually it was for doing the following program
/*program to find the maximum matching pattern in the string*/
#include < string.h >
#include < stdio.h >
#include < stdlib.h >
You can't have spaces after the '<' or before the '>' in a #include
directive. My compiler complains that it can't find headers
" string.h ", " stdio.h ", or " stdlib.h ". I'm surprised yours
accepts it, but since the interpretation of header names is
implementation-specific, it's possible. But the correct directives
are:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
Your article was encoded as "quoted-printable", which means that '='
characters are encoded as "=3D". If you can persuade your newsreaqder
to post plain text rather than quoted-printable, it would be helpful.
Possibly my own newsreader should have done the translation when
saving the file, but there's nothing in what you posted that really
requires anything more elaborate than a plain-text encoding.
void findmaxpat(char*,char*,int*);
void printmaxpat(char*,int*);
int main(void)
{
int i;
You declare ``i'', but you never use it.
Please, please, *PLEASE* indent your code properly. It's much more
difficult to read when everything is shoved over against the left
margin.
It's possible you did indent your code, and your newsreader or
something else killed it. If so, please try to solve that problem.
If you're using tabs for indentation, try using just spaces. And make
sure you're using an editor that's designed to work with source code.
[...]
gets(s);
Never use gets(). Never. See question 12.23 in the comp.lang.c FAQ,
<http://c-faq.com/>. (The FAQ is an excellent resource; reading the
whole thing would not be a waste of your time.)
[...]
(*maxpat == -1)? printf("\"%s\"\n",s) uts(s);
This is an entirely gratuitous use of the conditional operator; it
does nothing but make your code harder to read. Just use an if
statement:
if (*maxpat == -1)
printf("\"%s\"\n",s);
else
puts(s);
Personally, I always use braces, even for a single statement:
if (*maxpat == -1) {
printf("\"%s\"\n",s);
}
else {
puts(s);
}
but that's a fairly minor style point; I recommend it, but feel free
to make your own choice. Using "?:" rather than if/else in a
statement context, on the other hand, is just silly.
if(temp-s == *maxpat)
{
printf("^");
maxpat++;
}
else
printf("%c",' ');
}
puts("");
}
There are two main approaches to writing information to stdout. You
can use printf for everything, which is a simpler approach (as long as
you're careful about '%' characters with the format string). Or you
can make use of the make use of the wider variety of output functions,
such as puts, fputs, putchar, and so forth. Always using printf had
the advantage that there's only one function to learn. But here,
since you're using puts anyway, I'll suggest that the above 3 output
calls could be written as:
putchar('^');
...
putchar(' ');
...
putchar('\n');