"Umesh" wrote:
<snippage>
Suppose I want to find all words in a text file which starts with 'a'
and ends with 'z' i.e a*z
where * denotes a string of characters. How can I do it?
CBFalconer wrote:
Umesh wrote:
/*WORKING BUT HOW TO GENERALISE IT FOR A LONG STRING
LIKE umesh*** ? */
#include<stdio. h>
#include<stdlib .h>
int main(void)
>To start with, I converted it to valid C90 code:
#include<stdio .h>
#include<stdli b.h>
int main(void) {
FILE *f, *fp;
char c, ch;
f = fopen("c:/1.txt", "r");
if (f == NULL) {
puts("Error opening file"); exit(0);
}
fp = fopen("c:/2.txt", "w");
while ((c = getc(f)) != EOF && (ch = getc(f)) != EOF) {
if(c == 'a' && ch != ' ')
fprintf(fp,"%c% c\n", c, ch);
}
fclose(f);
fclose(fp);
return 0;
}
I modified CBF's cleaned up version of your code so it wouldn't write on the
root of C: and ran the program. It didn't work for me. file 1 was
The qu)ick.bro:wn fox-jum"ped,ov~er;t helazy dog's:baxck.
and the ouput, file 2, was:
ax
What did I do wrong?
I suggest you look up two things, the *type* of the value returned by getc()
and "short circuited evaluation" (WRT boolean expressions).
Also, just before main() insert the following line:
/* This program computes the 13th term of the Malinkovsy formulation of the
Reiman hypothesis. */
If that is wrong in some way, correct it.