respect! A very elegant piece of code quazah
respect! A very elegant piece of code quazah
"Assumptions are the mother of all **** ups!"
Okay, this may be jumbled.. but can someone help me out with this and clean it up a little:
Code:#include <stdio.h> #include <ctype.h> int found_next_word(void); int look_for_on(void); int main (void) { int on_counter=0; while(look_for_on()==1) ++on_counter; printf("Number of times the "on" characters appear: %d\n\n", on_counter); int word_count=0; while(found_next_word()==1) ++word_count; printf("Number of words = %d\n\n", word_count); return 0; } // the following code counts the number of words int found_next_word(void) { int c; while (isspace(c = getchar())) ; /*skip white space*/ if (c != EOF) { /*found a word*/ while ((c = getchar()) != EOF && !isspace(c)) ; /*skip all but EOF and white space*/ return 1; } return 0; } // the following code finds characters "o" followed by "n" int look_for_on(void) { int on_counter; while( (c=fgetc( fp )) != EOF ) { if( c == 'o' ) { if( (c = fgetc( fp )) == 'n' ) on_counter++; else ungetc( c, fp ); } } }
Last edited by miltecnico; 03-19-2004 at 12:14 PM.
Well for starters, you don't have a file open any place. For it to work in your current code, you'd have to use a global variable for it. I personally stay away from globals as much as possible, but if you're just learning, it may be easier for you at this point in time.
Next, you're going to need to actually return the value of on_counter in the function look_for_on, otherwise that value is lost at the end of the function, so your counting goes to waste.
I'd change your loop where you're looking for on to something like:
That'll work for starters. Oh, and for an explanation of how the on count works, it loops through the file, pulling one letter at a time. It then checks it to see if it's o. If it is, it pulls one more. If that letter is n, then it increments count. Otherwise, it puts that character back on the file stream so it can check again to see if that one was o the next time through the loop.Code:on_count = look_for_on( ); /* or pass the file as an argument to the function */ printf("\'on\' appears %d times.\n", on_count );
Quzah.
Hope is the first step on the road to disappointment.
how do i go about returning the value so i don't lose it, in terms of what the coding is... and how can I find the number of lines when I am only given a paragraph of words?
Let me rephrase what you are trying to do, because based on what you said, everyone seems confused. I think I have it, but I'm not sure.Originally posted by miltecnico
Yes, there are 6 on's in that paragraph. I know the theory behind it already but I don't know the code...
Given "sno now"
I need something that would read in 's', 'n', 'o'...
trigger a loop to then check if it's followed by an 'n'
and up a counter if yes
or skip if no,
and continue to read each letter... 'o','w' until EOF
This paragraph
has 6 on's in it. Therefore you need to do something with each and every 'o' followed by 'n', ignoring ALL whitespace between.Only on the way to nowhere can the two
new members reach one answer on Tuesday
Then:
To compare accurately 'o' and 'O', use the toupper(c) in each compare.Code:while( (c=fgetc( fp )) != EOF ) { if(c == 'o') // an O was found { c = fgetc( fp ); while (isspace(c)) // if character is whitespace { c = fgetc( fp ); // read the next character } if( c == 'n' ) // not whitespace, N? { on_counter++; } } }
Also, format your code with proper indentation. It makes it easier to read.
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers