Problem on finding frequency of an expression

This is a discussion on Problem on finding frequency of an expression within the C Programming forums, part of the General Programming Boards category; I have write this program but have an unexpected output. Can anyone help me to find what is the problem ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    Problem on finding frequency of an expression

    I have write this program but have an unexpected output. Can anyone help me to find what is the problem and modify it? I need to input text file in this program too.

    Code:
             while(fgets(str,300,infp)!=NULL)
             {
               puts(str);
             }
             printf("What expressions do you want to find? Please enter here: \n");
             gets(ex_str);
            
             if(strncmp(ex_str,str,strlen(ex_str))==0);
             count2++;
            
             printf("Total no of that expression appears %d times\n",count2);

  2. #2
    Registered User
    Join Date
    Nov 2007
    Location
    Bangalore, India
    Posts
    24
    What exactly are you trying to do here?
    Code:
    while(fgets(str,300,infp)!=NULL)
    {
       puts(str);
    }
    This would keep reading the input stream and putting the same to the output stream until an error happens with fgets. You should look for EOF.

    Code:
    if(strncmp(ex_str,str,strlen(ex_str))==0);
    count2++;
    I think semicolon ( ; ) at the end of if statement wasn't delibrate.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > gets(ex_str);
    Read the FAQ, gets() is dangerous.

    You should have used
    fgets( ex_str, sizeof ex_str, stdin );
    But also see the FAQ on dealing with the newline, if you need to.

    My guess is you want to ask "what to look for" before your while loop, then perform the comparison inside your while loop as you read each str from the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Sorry, i don't really understand what the use of
    fgets( ex_str, sizeof ex_str, stdin ); is. Would you mind explaining it for me?

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    fgets(ex_str, sizeof ex_str, stdin);

    reads input from standard input and stores it in ex_str, the sizeof ex_str argument ensures no overflow occurs.

    from code it looks like your trying to read in a file and searching the file for an expression and finally printing the number of times that expression appears.

    peter

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Code:
    fgets(ex_str, sizeof ex_str, stdin);
    Is basically the same as
    Code:
    gets(ex_str);
    Only fgets takes an argument of how many characters to read maximum. So you pass in your buffer size and it stops reading if there's more data to read than your buffer can hold. Thus no buffer overrun.

    If you're wondering what a buffer overrun is, you can check the link in my signature about scanf("%s"...).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21