Counting String Occurances

This is a discussion on Counting String Occurances within the C++ Programming forums, part of the General Programming Boards category; There are actually two parts to this problem. I've only begun to learn file handling, so as much help as ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Counting String Occurances

    There are actually two parts to this problem.

    I've only begun to learn file handling, so as much help as possible would be appreciated.

    What I need the program to first do is read the file until it finds the string "XMAP" and record its offset. Then I need it to count each occurance of the string "CMAP" before and after the offset in two different values.

    How to go about it?

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    have an integer initially set to 0 for counter and flag.
    read the file string by string
    if the current string is "XMAP" and the flag is zero, increase the flag by one
    while the flag > 0. if the current string is "CMAP", increment counter. Otherwise if it is XMAP, decrease the flag, return the counter.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'd say that depends very much on how large that file is.
    If it's small ( some megabytes ) then I would read it completely into memory( using fread() ), append a '\0' ( to make it a string ) and use strstr() to look for the strings.
    If it's a very large file ( wouldn't fit into memory ) you would have to read the file line by line and check the line for both substrings. ( using strstr() ) until you have found the first search string.
    Another possibility is to use a buffer of fixed size and use fread() to fill that buffer in a loop, but in this case you would have to take care about split seach strings at the end of the buffer.
    Kurt
    EDIT: Just noticed this is C++. getline() or fstream::read() and std::string functions would be better.
    Last edited by ZuK; 12-12-2006 at 09:20 AM.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    The files can vary from about 2 meg to 300 meg.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Well, you could keep the string in a FIFO queue... the queue is 4 chars long, search the queue for "XMAP", remove the first character from the queue and add the next character from the file to the back. You can use std::deque for a ready-made implementation.

    Code:
    std::deque <char> buffer;
    
    //read in the first 3 characters
    
    //loop
    while(!eof)
    {
        buffer.push_back(next_char)
        if("XMAP" in buffer)
        {
            //do something
        }
        buffer.pop_front()
    }

    you would have to read the file line by line and check the line for both substrings.
    If the file has VERY LONG LINES you'd be in for some trouble with this one. Eg. trying to read a UNIX-generated file using Windows EOL conversions.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    So how could I get the FIFO method to record the offset of the 'X' in "XMAP"?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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